2013-04-12 56 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>