展会信息港展会大全

Android使用xml改变button背景图片
来源:互联网   发布日期:2015-12-04 16:25:11   浏览:2217次  

导读:这篇文章我们使用Android XML来改变各种按钮状态(获得焦点、获得焦点并按下、失去焦点、默认)时的背景图,首先我们自己定义一个imgbutton.xml代码:?xml version=1.0 encoding=utf-8? selector xm......

这篇文章我们使用Android XML来改变各种按钮状态(获得焦点、获得焦点并按下、失去焦点、默认)时的背景图,首先我们自己定义一个imgbutton.xml代码:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- 获得焦点时 -->

<item

android:state_focused="true"

android:state_pressed="false"

android:drawable="@drawable/onfocusimage2"

/>

<!-- 获得焦点并按下 -->

<item

android:state_focused="true"

android:state_pressed="true"

android:drawable="@drawable/clickimage2"

/>

<!-- 失去焦点时 -->

<item

android:state_focused="false"

android:state_pressed="true"

android:drawable="@drawable/clickimage2"

/>

<!-- 默认时 -->

<item

android:drawable="@drawable/defaultimage2"

/>

</selector>

在main.xml中定义一个button代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/settingsLayoutId"

android:gravity="center">

<!--这个android:background就读取自己定义的XML文件 -->

<Button

android:background="@anim/imgbutton"

android:text="@string/app_pic"

android:id="@+id/imagesBut"

style="@style/settingsBut"

/>

</LinearLayout>

赞助本站

人工智能实验室

相关热词: xml button 背景图片

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港