10

I am having a linear layout in my xml and am adding another linear layout(containing two textviews) to that linear layout through java. Touch events works perfect, but i want to highlight the selected linear layout by setting background color. Please advice.

4

6 回答 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>

可绘制文件夹中的 normal.xml

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

可绘制文件夹中的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;        
        }
    });
于 2013-04-12T05:53:15.457 回答
14

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

XML:

android:clickable="true"

代码:

myLinearLayout.setClickable(true);
于 2014-05-25T15:00:31.570 回答
6

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

<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>
于 2016-09-14T08:03:24.117 回答
2

将 selector.xml 文件放入可绘制文件夹(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"
于 2013-04-12T05:49:51.830 回答
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();
}
于 2016-03-18T13:34:44.380 回答
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 
于 2013-04-12T05:50:01.520 回答