更改背景颜色onClick()

时间:2018-12-07 16:32:38

标签: android xml user-interface 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文件中的点击颜色?

1 个答案:

答案 0 :(得分:2)

这应该足够了

<?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