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

时间:2013-04-12 05:44:37

标签: android

我在xml中有一个线性布局,并通过java向该线性布局添加另一个线性布局(包含两个textview)。触摸事件非常完美,但我希望通过设置背景颜色来突出显示所选的线性布局。请指教。

6 个答案:

答案 0 :(得分:47)

在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"

您也可以将背景设置如下

触摸您的布局

  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;        
        }
    });

答案 1 :(得分:12)

无论您选择哪种方法(XML /代码),请务必使LinearLayout点击:{/ p>

XML:

android:clickable="true"

代码:

myLinearLayout.setClickable(true);

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

答案 3 :(得分:2)

这是我的解决方案,非常简单:

<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>

答案 4 :(得分:1)

以下方法可让您想要。甚至是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();
}

答案 5 :(得分:0)

尝试使用View函数setBackgroundColor()

请参阅Android文档:

http://developer.android.com/reference/android/view/View.html#setBackgroundColor%28int%29

public void setBackgroundColor (int color)       Added in API level 1
    Sets the background color for this view.
Parameters
    color   the color of the background