【问题标题】:change background Image textView when pressed [closed]按下时更改背景图像文本视图[关闭]
【发布时间】:2016-07-09 16:34:50
【问题描述】:

我使用这种方法,但它不起作用。 在drawable中我创建了selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="false"
        android:drawable="@drawable/reponseclicked">
    </item>
    <item
        android:drawable="@drawable/repons">
    </item>
</selector>

这是我的 TextView

 <TextView
    android:layout_width="160dp"
    android:layout_height="wrap_content"
    android:id="@+id/imageView8"
    android:text="text"
    android:gravity="center"
    android:layout_above="@+id/imageView5"
    android:background="@drawable/selector"/>

【问题讨论】:

  • 您能解释一下“不工作”吗?在那之前,这个问题不适合 SO,因为它没有 MCVE。

标签: android xml android-layout textview


【解决方案1】:

android:state_pressed="false" 更改为android:state_pressed="true"

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/reponseclicked" />
    <item
        android:drawable="@drawable/repons"/>
</selector>

See this Thread.

【讨论】:

    【解决方案2】:

    &lt;color name="colorControlHighlight"&gt;#B6B6B6&lt;/color&gt;

    这在你的 Drawable 中

    <item android:state_pressed="true">
    
        <shape>
            <solid android:color="@color/colorControlHighlight"/>
        </shape>
    </item>
    
    <item>
        <shape>
            <solid android:color="@color/colorControlHighlight"/>
        </shape>
    </item>
    
    <item android:drawable="@color/colorPrimary" />
    

    还有 这在你的 Drawable v21 中会给你涟漪效果

    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/colorControlHighlight">
    
        <item android:id="@android:id/mask" android:drawable="@android:color/white"/>
        <item android:drawable="@color/colorPrimary" />
    </ripple>
    

    你的文本视图

    <TextView
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView8"
        android:text="text"
        android:gravity="center"
        android:layout_above="@+id/imageView5"
        android:background="@drawable/selector"
        android:clickable="true"/>
    

    【讨论】:

      【解决方案3】:

      TextView 默认是不可点击的。所以首先启用它们,如下所示。

      android:clickable="true"
      android:focusable="true"
      android:focusableInTouchMode="true"
      

      【讨论】:

        猜你喜欢
        • 2013-11-04
        • 1970-01-01
        • 2014-07-29
        • 2014-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-08
        相关资源
        最近更新 更多