2012-03-16 66 views
1

我有一些ListView。其中的所有項目都是矩形形狀,但頂部有一個圓角,如this照片所示。爲了創建它,我已經用角切割了頂部條紋,並將其保存爲item_bg_white_top圖像和1px高度的條紋保存爲item_bg_white_line圖像。這就是我構建它的方式。如何點擊複雜的LinearLayout就像點擊ListView項目一樣點擊閃光效果?

我想點擊ltest佈局,就像點擊ListView項目一樣做閃光效果。

我該怎麼做?

我試過下面的代碼ltest但它沒有幫助。當我在ltest_inner上試用此代碼時,它只是將其背景更改爲黑色。

final LinearLayout ll = (LinearLayout)findViewById(R.id.ltest);  
    ll.setOnClickListener(new View.OnClickListener() {   
     public void onClick(View v) {    
      ll.setBackgroundResource(android.R.drawable.list_selector_background);    
     } 
    }); 

item.xml

<?xml version="1.0" encoding="utf-8"?> 
      <LinearLayout 
       android:id="@+id/ltest" 
       android:layout_width="300dp" 
       android:layout_height="33dp" 
       android:orientation="vertical"> 

       <LinearLayout 
        android:id="@+id/ltest_inner" 
        android:layout_width="300dp" 
        android:layout_height="8dp"     
        android:background="@drawable/item_bg_white_top" /> 

       <LinearLayout 
        android:layout_width="300dp" 
        android:layout_height="25dp" 
        android:background="@drawable/item_bg_white_line_repeat" > 
       </LinearLayout> 
      </LinearLayout> 

item_bg_white_line_repeat.xml

<?xml version="1.0" encoding="utf-8"?> 
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/item_bg_white_line" 
    android:tileMode="repeat" /> 

回答

0

你想要做的就是建立一個選擇器,並用它作爲繪製的背景是什麼。在繪製文件夾

<?xml version="1.0" encoding="utf-8"?> 
     <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
      <item android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/disabled_pressed_image" /> 
      <item android:state_enabled="false" android:state_focused="true" android:drawable="@drawable/enabled_focused_image" /> 
      <item android:state_enabled="false" android:drawable="@drawable/enabled_image" /> 
      <item android:state_focused="true" android:drawable="@drawable/focused_image" /> 
      <item android:state_pressed="true" android:drawable="@drawable/pressed_image" /> 
      <item android:drawable="@drawable/default_image" /> 
</selector> 

上述參考圖像也可繪製的:

在繪製文件夾中創建一個XML文件,並選擇添加到它。你不必實現所有的狀態。這些只是一些可能的組合。

然後附上這個爲背景,以您的線性佈局:

<LinearLayout 
    android:id="@+id/ltest" 
    android:layout_width="300dp" 
    android:layout_height="33dp" 
    android:background="@drawable/selector_file_name" 
    android:orientation="vertical"> 

其中選擇文件名就是你給我上面提到的選擇XML文件的名稱。

還考慮刪除內部LinearLayouts。你可以用9-patch做你想做的事情,那麼你只會有一個LinearLayout(如果它不打算託管其他視圖,你可以改變爲ImageView)。

+0

但是,如果''test_inner'沒有按下但是隻有'ltest'被按下,'state_pressed'將如何受到影響? – theateist 2012-03-16 01:17:35