您的位置:首页 > 其它

选中时和未选中时的背景颜色和文字颜色的改变

2017-12-27 23:53 441 查看
1.在你的项目Res--drawable--选中右键new--Drawable resource file

2.在里面写代码

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--true  是你选中时的状态   drawable="@color/baise"  这里你也可以换成一张图片-->
<item android:drawable="@color/baise" android:state_checked="true" />
<item android:drawable="@color/lanse" android:state_checked="false" />
</selector>


3.在布局中调用

4.这是另外一个的用法  接着就是往里面添加代码

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 这个是设置控件的圆角-->
<corners android:topLeftRadius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp"/>
<!--这是给控件描边-->
<stroke android:width="0.1dp"
android:color="#f5f5f5"
></stroke>
<!--这是设置背景或者文字颜色-->
<solid android:color="#f5f5f5" />
</shape>


5.在布局中调用

6.EditText输入时改变颜色  drawable文件夹内的my_image.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_focused="true" android:color="#f44" />
<item android:state_focused="false" android:color="#111" />

</selector>

7.在布局中使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/my_image"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/my_image"
/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: