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

时间:2011-05-20 05:40:46

标签: android

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

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

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

3 个答案:

答案 0 :(得分:2)

您可以使用XML drawable,
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

像这样创建一个xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

你需要背景图片

答案 1 :(得分: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>

我在本例中使用LinearLayout。我设置android:clickable="true"来激活点击状态... 这也可以在ListView中使用...只需在自定义行项目...

上使用它

抱歉我的英语语法不好

答案 2 :(得分:0)

@nagesh 此链接将解决您的问题OnClick change tablerow background color

相关问题