如何更改焦点视图的背景颜色

nge*_*esh 6 android

我想改变焦点下视图的颜色; 在我的情况下,它是一个表格行.

这是我目前的代码,但它不起作用.

            r.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1) {
                {               {
                    arg0.setBackgroundColor(Color.LTGRAY);
                    r.setFocusableInTouchMode(true);
                }
            return false;
        }});
Run Code Online (Sandbox Code Playgroud)

Tek*_*Yin 1

在这里我为你做了一些例子... http://life-as-a-coder.com/_shared/background_state.rar

看看我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="@drawable/item_background"
        android:clickable="true">
        <TextView
            android:text="Item 1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35dip"></TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="@drawable/item_background"
        android:clickable="true">
        <TextView
            android:text="Item 2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="35dip"></TextView>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我在这个例子中使用 LinearLayout。我设置android:clickable="true"为激活单击状态...这也可以在 ListView 中使用...只需在自定义行 Item 上使用它...

抱歉我的英语和语法不好