【问题标题】:android linearlayout background selectorandroid线性布局背景选择器
【发布时间】:2011-09-26 08:15:06
【问题描述】:

我通过动态膨胀它们在滚动视图中添加一些线性布局视图。我已将添加的 LinearLayout 的背景设置为选择器列表。但是添加到滚动视图后,当我按下所选视图时,它不会显示选择器列表的任何影响。我使用的示例 XML 是:

选择器文件: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 线性布局:

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

上面的膨胀视图被添加到的 ScrollView 是:

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

任何想法...???

【问题讨论】:

  • 把它放在scrollview之外还能用吗?
  • 我猜滚动视图中的某些控件会消耗按下事件,因此您的孩子 LinearLayout 无法触摸。
  • 实际上我将选择器设置为线性布局的背景。如果我对按钮使用相同的选择器,它可以工作,但不适用于 LinearLayout...
  • 谢谢大家。我有解决办法。我在膨胀文件的线性布局中添加了以下行: android:clickable="true" android:focusable="true" android:focusableInTouchMode="true"
  • android:clickable="true" 修复了按钮按下时看起来没有按下的问题。但是如果你使用 setSelected(true) 那么 state_selected drawable 就不再起作用了......

标签: android selector inflate


【解决方案1】:

您必须使 LinearLayout 可点击。

android:clickable="true"

【讨论】:

  • 非常感谢,我错过了相对布局
  • 当我点击一个布局时,所有布局都显示按下状态。这也解决了这个问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多