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

   2024-10-01 5930
核心提示:在Android布局文件中,要引用一个StateListDrawable,你需要先在res/drawable目录下创建一个XML文件,该文件定义了StateListDraw

在Android布局文件中,要引用一个StateListDrawable,你需要先在res/drawable目录下创建一个XML文件,该文件定义了StateListDrawable。然后,在布局文件中使用这个StateListDrawable作为视图的背景或者其他属性。

以下是一个简单的示例:

首先,在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>
然后,在布局文件中引用这个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,并将其设置为按钮的背景。

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号