溫馨提示×

Android button顏色怎么設置

小億
265
2024-03-01 09:32:19
欄目: 編程語言

要設置Android按鈕的顏色,可以使用以下方法:

  1. 在XML布局文件中設置按鈕的背景顏色:
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:background="@color/colorPrimary" />
  1. 在java代碼中設置按鈕的背景顏色:
Button button = findViewById(R.id.button);
button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
  1. 使用Selector設置按鈕的不同狀態下的顏色: 在res/drawable文件夾下創建一個selector文件,例如button_selector.xml,內容如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/colorAccent" />
    <item android:drawable="@color/colorPrimary" />
</selector>

然后在布局文件或java代碼中設置按鈕的背景為這個selector:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:background="@drawable/button_selector" />

通過以上方法可以設置Android按鈕的顏色。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女