2013-04-12 55 views
7

我在我的xml中有一个线性布局,并且正在通过java向该线性布局添加另一个线性布局(包含两个textview)。触摸事件的作品完美,但我想通过设置背景色来突出显示选定的线性布局。请指教。在绘制文件夹(RES /绘制)单击时更改线性布局背景颜色

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

+3

张贴代码你尝试了什么 –

+0

为'LinearLayouts'实现Touch或Click事件,并根据布局的选择更改背景颜色。 – GrIsHu

+0

Raghunandan有很好的答案 - 你应该选择它 – AndrewS

回答

1

放selector.xml文件和XML文件的线性布局设置背景

android:background="@drawable/background" 
+0

不正确,你不能命名文件selector.xml然后调用@ drawable/background并且期望它能够工作 –

45

抽拉夹

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

然后设置定义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背景给你布局

android:background="@drawable/background" 

您还可以设置背景如下

在布局的触摸无论您选择的方法(XML /代码)

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

- 确保你让你的LinearLayout clickable:

XML:

android:clickable="true" 

代码:

myLinearLayout.setClickable(true); 
+0

谢谢!它为我工作 –

1

下面的方法将得到你想要的,你想要的。即使是棒棒糖中的涟漪动画。只是通过上下文的视图(可以是线性布局):

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

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

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