溫馨提示×

android statelistdrawable在布局文件中的引用方式

小樊
90
2024-08-27 19:26:04
欄目: 編程語言

在Android布局文件中,要引用一個StateListDrawable,你需要先在res/drawable目錄下創建一個XML文件,該文件定義了StateListDrawable。然后,在布局文件中使用這個StateListDrawable作為視圖的背景或者其他屬性。

以下是一個簡單的示例:

  1. 首先,在res/drawable目錄下創建一個名為button_background.xml的文件,定義StateListDrawable:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <!-- Color when button is pressed -->
       <shape>
            <solid android:color="@android:color/holo_blue_dark"/>
            <corners android:radius="4dp"/>
        </shape>
    </item>
    <item>
        <!-- Default button color -->
       <shape>
            <solid android:color="@android:color/holo_blue_light"/>
            <corners android:radius="4dp"/>
        </shape>
    </item>
</selector>
  1. 然后,在布局文件中引用這個StateListDrawable:
    android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:background="@drawable/button_background" />

在這個示例中,我們創建了一個StateListDrawable,當按鈕被按下時,它的背景色變為深藍色,否則為淺藍色。然后,我們在布局文件中引用了這個StateListDrawable,并將其設置為按鈕的背景。

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