我将列表查看项目添加到listview项目xml布局后,我的列表视图项目不再可点击,如果我点击列表视图中的任何项目后背景剂量不受影响
我的代码中的问题在哪里以及如何修复它,我的意思是我可以在forum_box_bi.xml
资源中添加onclick背景颜色,而不会丢失当前设置吗?
这是listview项目xml layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e3e4e6"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/thread_bit_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dip"
android:layout_marginTop="3dip"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:background="@drawable/forum_box_bit"
android:padding="5dip"
android:orientation="vertical" >
<!-- Post Aavatar + time + username -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/postbit_useravatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="true"
android:focusable="true"
android:src="@drawable/no_avatar" />
<TextView
android:id="@+id/postbit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:layout_toLeftOf="@+id/postbit_useravatar"
android:ellipsize="end"
android:singleLine="true"
android:text="sss"
android:textDirection="rtl"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
<ImageView
android:id="@+id/postbit_boxmenu"
android:clickable="true"
android:focusable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/mini_menu" />
<TextView
android:id="@+id/postbit_timestamp"
android:layout_marginRight="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#b9b9b9"
android:layout_toLeftOf="@+id/postbit_useravatar"
android:layout_below="@+id/postbit_username"
android:text="timestamp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
<!-- // Post User Aavatar + time + username -->
</LinearLayout>
</LinearLayout>
这是forum_box_bit.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#c4c5c6" />
<corners android:radius="1dp"/>
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#ffffff"/>
<corners android:radius="1dp"/>
</shape>
</item>
</layer-list>
答案 0 :(得分:1)
通常您必须将选择器设置为背景。什么是“forum_box_bit”,这是一个选择器吗?如果没有在您的drawables文件夹中定义一个,如下例所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_pressed="false"
android:drawable="@drawable/default_shape"></item>
<item
android:state_pressed="true"
android:drawable="@drawable/pressed_shape"></item>
<item
android:drawable="@drawable/default_shape"></item>
</selector>
您可以为图像定义背景或为此选择器创建一些xml形状。
对于某些形状,为可绘制文件夹中的按下状态和未按下状态定义一个形状:
按下(pressed_shape.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:startColor="@color/dark_orange_pressed"
android:endColor="@color/light_orange_pressed"/>
<corners
android:radius="5dp"/>
<stroke
android:color="@color/light_gray"
android:width="2dp"/>
</shape>
未压缩(default_shape.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:startColor="@color/dark_orange"
android:endColor="@color/light_orange"/>
<corners
android:radius="5dp"/>
<stroke
android:color="@color/light_gray"
android:width="2dp"/>
</shape>
并定义颜色,在您的values文件夹中创建一个xml,它必须命名为“colors.xml”:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="dark_orange">#f6801c</color>
<color name="light_orange">#f1a84a</color>
<color name="light_gray">#cfcfcf</color>
<color name="dark_orange_pressed">#b07d3b</color>
<color name="light_orange_pressed">#d29648</color>
</resources>