Android - TextView背景颜色在不同状态下更改时出现问题

时间:2010-12-03 12:14:23

标签: android textview

请尝试下面的代码并指导我,我的问题是我没有得到TextView的“按下”状态,是否可以为TextView实现Pressed状态?我如何能?

我使用下面的Button代码但没有成功使用TextView。

请指导我

我的按钮和TextView代码:

<Button 
    android:id="@+id/Button01" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp"
    android:background="@drawable/button_state"
    android:padding="20dp">
</Button>

<TextView 
    android:text="Demo of Color on state change"  
    android:id="@+id/TextView01" 
    android:layout_width="fill_parent" 
    android:layout_height="50dp"
    android:background="@drawable/button_state"
    android:focusable="true">
</TextView>

button_state.xml 文件(我已将其存储在可绘制文件夹中):

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

    <item android:state_pressed="true">
        <shape>
            <gradient
                android:startColor="@color/yellow1"
                android:endColor="@color/yellow2"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/grey" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <gradient
                android:endColor="@color/orange1"
                android:startColor="@color/orange2"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/grey" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:endColor="@color/blue1"
                android:startColor="@color/blue2"
                android:angle="270" />
            <stroke
                android:width="3dp"
                android:color="@color/grey" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

color.xml 档案:

<resources>
<color name="yellow1">#FFFF99</color>
<color name="yellow2">#FFCC00</color>
<color name="orange1">#FF9966</color>
<color name="orange2">#FF6600</color>
<color name="blue1">#99CCFF</color>
<color name="blue2">#0033CC</color>
<color name="grey">#736F6E</color>
</resources>

1 个答案:

答案 0 :(得分:4)

AFAIK,TextView不支持按下状态。

只需使用Button即可。由于您要更改Button背景,因此您可以根据需要进行查看,包括看起来像普通的TextView