繁体   English   中英

更改背景颜色onClick()

[英]Change background colour onClick()

我有一个ImageView和一个tag_layout.xml

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

    <solid android:color="#272822">

    </solid>

    <stroke android:color="#686868" android:width="1dp">

    </stroke>

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

我应该对以下内容进行哪些更改:单击imageview时更改背景的背景色。 我已经使用了这个tag_layout作为:

<ImageView
            android:background="@drawable/tag_layout"
            android:padding="5dp"
            android:scaleType="fitCenter"
            android:layout_marginRight="5dp"
            android:layout_width="0dp"
            android:layout_height="37dp"
            android:src="@drawable/zoomin"
            android:layout_weight="1"
            />

如何在xml文件中分配点击颜色?

这样就足够了

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true">
       <shape android:shape="rectangle">
           <solid android:color="#yourcoloronpressed"/>
           <stroke android:color="#686868" android:width="1dp"/>
           <corners android:radius="8dp"/>
       </shape>
   </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#272822"/>
            <stroke android:color="#686868" android:width="1dp"/>
            <corners android:radius="8dp"/>
        </shape>
    </item>
</selector>

用您的颜色替换yourcoloronpressed

当然,您还可以更改其他属性,例如按下时的strokecorners

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM