开发者

Android - problem with TextView background color change on the different state of it

In the below code, my problem is that I am not getting the "Pressed" state for the TextView, is it possible to implement Pressed state for the TextView? How can I do that?

I am having a success using the below code for the Button but not having success for the TextView.

My Button and TextView code:

<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 file (I have stored it in drawable folder):

<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"
             开发者_C百科   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 file:

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


AFAIK, TextView does not support the pressed state.

Just use a Button. Since you are changing the Button background, you can make it look however you want, including looking like a plain TextView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜