[JS] selector 背景选择器
发布日期:2021-09-05 08:11:49 浏览次数:2 分类:技术文章

本文共 2827 字,大约阅读时间需要 9 分钟。

用于listview和button改变android原来控件的背景

android的selector是在drawable/xxx.xml中配置的

 

1、定义xml

把下面的XML文件保存成你自己命名的.xml文件,在使用时根据ListView中的列表项的状态(按住/松开/触摸等)来使用相应的背景图片。

drawable/list_item_bg.xml

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

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

    <!-- 默认时的背景图片-->

    <item android:drawable="@drawable/pic1" />

    <!-- 没有焦点时的背景图片-->

    <item android:state_window_focused="false" android:drawable="@drawable/pic1" />

    <!-- 非触摸模式下获得焦点并单击时的背景图片-->

    <item android:state_focused="true" android:state_pressed="true"

        android:drawable="@drawable/pic2" />

    <!-- 触摸模式下单击时的背景图片-->

    <item android:state_focused="false" android:state_pressed="true"

        android:drawable="@drawable/pic3" />

    <!--选中时的图片背景-->

    <item android:state_selected="true" android:drawable="@drawable/pic4" />

    <!--获得焦点时的图片背景-->

    <item android:state_focused="true" android:drawable="@drawable/pic5" />

</selector>

 

2、使用xml

(1)listView

① 在listview中配置android:listSelector="@drawable/list_item_bg"

② 在listview的item中添加属性android:background=“@drawable/list_item_bg"

③ 在java代码中使用:

  Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg); 

      ListView.setSelector(drawable);

但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent" 使其透明。

 

(2)Button

再来看看Button的一些背景效果:

android:state_selected是选中

android:state_focused是获得焦点

android:state_pressed是点击

android:state_enabled是设置是否响应事件,指所有事件

根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。

 

以下就是配置button中的文字效果:

drawable/button_font.xml

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

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

    <item android:state_selected="true" android:color="#FFF" />

    <item android:state_focused="true" android:color="#FFF" />

    <item android:state_pressed="true" android:color="#FFF" />

    <item android:color="#000" />

</selector>

Button还可以实现更复杂的效果,例如渐变啊等等。

 

drawable/button_color.xml 

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

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

    <item android:state_pressed="true">

        <!-- 定义当button 处于pressed 状态时的形态。-->

        <shape>

            <gradient android:startColor="#8600ff" />

            <stroke android:width="2dp" android:color="#000000" />

            <corners android:radius="5dp" />

            <padding android:left="10dp" android:top="10dp"

                android:bottom="10dp" android:right="10dp" />

        </shape>

    </item>

    <item android:state_focused="true">

        <!-- 定义当button获得focus时的形态-->

        <shape>

            <gradient android:startColor="#eac100" />

            <stroke android:width="2dp" android:color="#333333" color="#ffffff" />

            <corners android:radius="8dp" />

            <padding android:left="10dp" android:top="10dp"

                android:bottom="10dp" android:right="10dp" />

        </shape>

    </item>

</selector>

 

最后,需要在包含button的xml文件里添加两项。假如是main.xml 文件,我们需要在<Button />里加两项。

     android:focusable="true"

     android:backgroud="@drawable/button_color"

这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊

转载地址:https://blog.csdn.net/weixin_34235135/article/details/85616622 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Choosing an ORM strategy
下一篇:How to Programmatically Switch between the HubTile Visual States

发表评论

最新留言

不错!
[***.144.177.141]2024年04月04日 08时49分32秒