style of selector does not work

回眸只為那壹抹淺笑 提交于 2019-12-11 06:46:10

问题


my style for text

<style name="TextStylePressed">
    <item name="android:textSize">20px</item>
    <item name="android:gravity">center_horizontal</item>
    <item name="android:textColor">#fff</item>
    <item name="android:textStyle">bold</item>
</style>

My selector arrow.xml where I use a style

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item style="@style/TextStylePressed">
        <shape>
            <gradient android:angle="90.0" android:endColor="#F1F4F2" android:startColor="#F1F4F2" android:type="linear" />

            <corners android:bottomLeftRadius="7dip" android:topLeftRadius="7dip" />
        </shape>
     </item>
</selector>

and a list of where I use the selector

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/arrow"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|left"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvDescr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="wwwwww" >
        </TextView>
    </LinearLayout>

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:maxHeight="20dp"
        android:minHeight="20dp" >
    </ImageView>

</LinearLayout>

as you can see the style has not changed


回答1:


Try adding android:state_pressed="true" to your selector item.

EDIT: I also came across this exact problem the other day. Try setting android:duplicateParentState="true" in the views that need to have their states duplicated. It worked for me the other day.




回答2:


  • Your arrow is supposed to become a particular style when it gets pressed;
  • Your arrow is used as background for linear layout;
  • Questions: - have you set linear layout to be clickable? - are you changing the arrow style via Java? [If yes, please provide code sample].


来源:https://stackoverflow.com/questions/12387487/style-of-selector-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!