【问题标题】:Change background colour onClick()更改背景颜色 onClick()
【发布时间】:2018-12-07 16:32:38
【问题描述】:

我有一个 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 文件中指定点击颜色?

【问题讨论】:

  • 您想永久更改视图的颜色,还是只要按下它就可以?
  • 只要按下

标签: android xml user-interface onclick


【解决方案1】:

应该够了

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

【讨论】:

  • 成功了!我做了几乎同样的事情,但没有改变可点击和焦点能够为真,所以它不起作用。无论如何,非常感谢
  • 没问题。很高兴我能帮忙
猜你喜欢
  • 1970-01-01
  • 2019-06-27
  • 2015-02-12
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多