目錄
一、Button是什么?
二、使用步驟
1.demo
一、Button是什么?
Button控件具有許多屬性可以用來(lái)自定義其外觀(guān)和行為。以下是Button控件常用的參數(shù)屬性及其詳解:
-
android:id
:設(shè)置Button的唯一標(biāo)識(shí)符。在代碼中可以使用這個(gè)ID引用Button。 -
android:layout_width
:指定Button的寬度,可以使用以下值:- "wrap_content":根據(jù)按鈕內(nèi)部?jī)?nèi)容自適應(yīng)寬度。
- "match_parent":填充父布局的寬度。
- 具體數(shù)值(如"100dp"):設(shè)置具體的寬度值。
-
android:layout_height
:指定Button的高度,可以使用以下值:- "wrap_content":根據(jù)按鈕內(nèi)部?jī)?nèi)容自適應(yīng)高度。
- "match_parent":填充父布局的高度。
- 具體數(shù)值(如"50dp"):設(shè)置具體的高度值。
-
android:text
:設(shè)置Button顯示的文本內(nèi)容。 -
android:textColor
:設(shè)置Button文本的顏色。- 可以是具體顏色值(如"#FF0000"表示紅色)。
- 可以是指向顏色資源的引用(如"@color/red"),資源文件中定義對(duì)應(yīng)的顏色值。
-
android:textSize
:設(shè)置Button文本的大小。- 可以是具體數(shù)值(如"14sp")。
- 也可以是指向尺寸資源的引用。
-
android:background
:設(shè)置Button的背景,可以使用以下值:- 可以是具體顏色值(如"#FF0000"表示紅色)。
- 可以是指向圖片資源的引用(如"@drawable/button_bg"),使用圖片作為背景。
-
android:padding
:設(shè)置Button的內(nèi)容與邊框之間的內(nèi)邊距,可以使用以下值:- 具體數(shù)值(如"10dp"):設(shè)置相同的上下左右內(nèi)邊距。
- 四個(gè)數(shù)值(如"5dp, 10dp, 5dp, 10dp"):分別設(shè)置上、右、下、左方向的內(nèi)邊距。
-
android:gravity
:設(shè)置Button中文本內(nèi)容的對(duì)齊方式。- 可以使用以下值組合來(lái)實(shí)現(xiàn)不同的對(duì)齊方式:
- "center":在水平和垂直方向上居中對(duì)齊。
- "left":在水平方向上左對(duì)齊。
- "right":在水平方向上右對(duì)齊。
- "top":在垂直方向上頂部對(duì)齊。
- "bottom":在垂直方向上底部對(duì)齊。
- 可以使用以下值組合來(lái)實(shí)現(xiàn)不同的對(duì)齊方式:
-
android:onClick
:設(shè)置Button點(diǎn)擊事件的回調(diào)方法。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-719275.html- 在代碼中需要定義對(duì)應(yīng)的方法,并在該屬性中指定方法名。
- 點(diǎn)擊Button時(shí),系統(tǒng)會(huì)調(diào)用指定的方法進(jìn)行響應(yīng)。
這些是常用的。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-719275.html
二、使用步驟
1.demo
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".DictionaryTableActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="MissingConstraints"> <Button android:id="@+id/button_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="button hello world" tools:ignore="MissingConstraints" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
public class DictionaryTableActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dictionary_table);
Button button = (Button) findViewById(R.id.button_2);
button.setOnClickListener(this);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_2:
Toast.makeText(DictionaryTableActivity.this,"Hello world",Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
到了這里,關(guān)于[Android studio] 第6節(jié) Button控件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!