点击时更改线性布局的背景颜色

10

我在xml中使用了线性布局,并通过Java向该线性布局添加另一个包含两个文本视图的线性布局。触摸事件工作正常,但我想通过设置背景颜色来突出显示所选的线性布局。请给予建议。


4
请发布你尝试过的代码。 - Sankar V
LinearLayouts实现触摸或点击事件,并根据布局的选择更改其背景颜色。 - GrIsHu
Raghunandan的回答非常好 - 你应该选择它。 - AndrewS
6个回答

53

在drawable文件夹中定义background.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/pressed" />
<item  android:state_focused="false" 
    android:drawable="@drawable/normal" />
</selector>

drawable文件夹中的normal.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/>    
</shape>

在drawable文件夹中的pressed.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FF1A47"/>      
</shape>

然后将背景设置为您的布局。

  android:background="@drawable/background"
你也可以像下面这样设置背景色:
在你的布局上添加Touch事件。
  ll.setOnTouchListener( new View.OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            switch(event.getAction())
            {
        case MotionEvent.ACTION_DOWN:
            ll.setBackgroundColor(Color.RED);
            break;
            case MotionEvent.ACTION_UP:

            //set color back to default
            ll.setBackgroundColor(Color.WHITE);  
            break;
            }
            return true;        
        }
    });

14
无论您选择哪种方法(XML/代码)- 请确保让您的LinearLayout可点击:

XML:

android:clickable="true"

代码:

myLinearLayout.setClickable(true);

7
这是我的解决方案,非常简单:
<LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bg_pressed_tab"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/app_name"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/service_request"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_small" />

            <requestFocus />
        </LinearLayout>

bg_tab_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_longAnimTime">

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

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

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

    <item android:drawable="@color/colorPrimary" android:state_focused="false" android:state_pressed="false" />

</selector>

这个解决方案完美地工作,并在您拥有2个或更多此类视图组并且您只想突出显示其中一个时切换背景。但是,在某些情况下,它将失败,例如如果布局中有其他视图(如编辑文本)会夺走焦点。 - Khay

2
以下方法可以帮助您达到想要的效果,甚至包括Lollipop中的涟漪动画。只需将上下文传递给视图(可以是线性布局)即可:
public static void setClickableAnimation(final Context context, final View view) {
    final int[] attrs = new int[]{R.attr.selectableItemBackground};
    final TypedArray typedArray = context.obtainStyledAttributes(attrs);
    final int backgroundResource = typedArray.getResourceId(0, 0);
    view.setBackgroundResource(backgroundResource);
    typedArray.recycle();
}

2
将selector.xml文件放在drawable文件夹中(res/drawable)。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:drawable="@drawable/list_focused" android:state_pressed="true"/>
    <item android:drawable="@drawable/list_focused" android:state_enabled="true" android:state_focused="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/list_raw_img"/>

</selector>

在xml文件中设置线性布局的背景

android:background="@drawable/background"

1
错误,你不能将文件命名为selector.xml,然后调用@drawable/background并期望它能正常工作。 - Gustavo Baiocchi Costa

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接