2011-09-26 158 views
12

我在滾動視圖中添加一些Linearlayout視圖,通過動態膨脹它們。我已將添加的LinearLayout的背景設置爲選擇器列表。但是在添加到scrollview後,當我按下所選視圖時,它不會顯示選擇器列表的任何影響。我使用的示例個XML是:android linearlayout背景選擇器

選擇的文件:selector_file

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/listbg" 
     android:state_pressed="true" /> 
<item android:drawable="@drawable/bgsmall"/> 
</selector> 

而且我充氣下面的視圖和添加到L1的LinearLayout:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/selector_file" 
android:gravity="center_vertical" 
> 
<ImageView android:id="@+id/image1" android:layout_width="100dip" android:layout_height="75dip"/> 
<TextView android:id="@+id/textitem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000"></TextView> 
</LinearLayout> 

而滾動型,其在上述正在添加的充氣視圖是:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
> 

<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"> 
    <LinearLayout android:id="@+id/l1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> 

</LinearLayout> 
</ScrollView> 
</LinearLayout> 

任何想法...?

+0

它工作,如果你把它放在滾動視圖之外? – blessenm

+0

我想scrollview中的一些控件會消耗按下的事件,所以你的孩子的LinearLayout無法觸摸。 – anticafe

+1

其實我將選擇器設置爲Linearlayout的背景。如果我爲按鈕使用相同的選擇器,它可以工作,但不適用於LinearLayout ... –

回答

28

您必須使LinearLayout可點擊。

android:clickable="true" 
+0

非常感謝,我錯過了設置相對佈局 – Zoombie