如何更改表格行的选定背景颜色

时间:2014-07-14 06:40:30

标签: android android-layout android-xml

我有一个TableLayout,每行有三个TextView。我试图改变背景颜色并突出显示所选行的颜色,但它没有改变。我在colors.xml中定义了值中的绿色/红色/蓝色。但是当我运行我的应用程序并单击该行时,没有任何变化,背景颜色保持透明。我已将透明更改为红色,即使单击后颜色仍保持红色。所以我知道表行已正确连接到row_selector.xml,但它无法正常工作。

这是主要的xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
      tools:context=".MainActivity"
       android:layout_width="wrap_content"
    android:layout_height="match_parent">

<TableLayout  


    android:layout_width="match_parent"
    android:layout_height="match_parent"

   >

                 <LinearLayout android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">



 <TableRow
     android:id="@+id/one"
      android:focusable="true"
   android:layout_width="fill_parent"
      android:layout_height="wrap_content"              

     android:background="@drawable/selector"

     android:onClick="rowClick" >

       <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="User Name"
                  android:textSize="20sp"

                  android:textStyle="bold" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="fill_parent"

                android:layout_height="wrap_content"
                android:text="" />

        </LinearLayout>

        </TableRow>



 <TableRow   
        android:id="@+id/two"
        android:onClick="rowClick"
         android:focusable="true"

        android:layout_width="fill_parent"
                android:layout_height="wrap_content"        
        android:background="@drawable/selector"

        >

       <LinearLayout android:orientation="vertical"

            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/textView3"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Display Name "
                 android:textSize="20sp"
                  android:textStyle="bold" />

            <TextView
                android:id="@+id/textView4"

                android:layout_width="48dp"
                android:layout_height="wrap_content"
                android:text="" />
        </LinearLayout>

        </TableRow>


    </LinearLayout>
</ScrollView>

</TableLayout>

这是选择器xml

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


     <item android:drawable="@android:drawable/list_selector_background"></item>

</selector>

这是list_selector_backgroung

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

    <item android:state_selected="true" 
          android:state_pressed="false"
          android:drawable="@color/grey"></item>


    <item android:state_pressed="true"
          android:state_pressed="false"
          android:drawable="@color/green"></item>   


    <item android:drawable="@color/black"></item>   <!-- for other state -->        
</selector>

这是颜色xml

<color name="safe_mode_text">#80ffffff</color>
    <color name="red">#FF0000</color>
    <color name="green">#008000</color>
    <color name="white">#ffffffff</color>
    <color name="grey">#808080</color>
    <color name="black">#ff000000</color>
    <color name="transparent">#00000000</color>

1 个答案:

答案 0 :(得分:0)

尝试用这样的

替换你的selector.xml
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/grey" android:state_pressed="true"/>

    <item android:drawable="@color/green" android:state_focused="true"/>

    <item android:drawable="@color/black"/>//default

    </selector>

编辑:执行操作,直接使用@color / ...