【问题标题】:ListView item background using custom selector使用自定义选择器的 ListView 项目背景
【发布时间】:2012-11-29 01:42:25
【问题描述】:

我意识到这个问题有很多主题,我都看过了。但是我仍然很困惑为什么我的 ListView 背景仍然是黑色的。只有当我将构成我的 ListView 行的 LinearLayout 的背景设置为某种颜色时,我才能更改背景。但是,当我将 LinearLayout 的背景设置为颜色时,我无法查看选择器。

我设置了cacheColorHint="#00000000",android:scrollingCache="false",甚至尝试了android:drawSelectorOnTop="true"。我相信问题可能出在定义或创建我的选择器的某个地方,因为当我测试应用程序时,我从未能够在屏幕上查看我的更改。但这都是猜测,因为我无法解决我的问题,我真的不知道出了什么问题。下面你可以查看我的代码。

我的选择器:list_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:color="#80ff0000" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:color="#80ff0000" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:color="#80ff0000" /> <!-- pressed -->
<item android:color="#ffffffff" /> <!-- default -->
</selector>

我的列表视图:main.xml

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

<!-- List view -->
<ListView android:id="@+id/android:list" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:dividerHeight="0px"
    android:divider="#0099CC"
    android:listSelector="@drawable/list_background"
    android:drawSelectorOnTop="true"
    android:cacheColorHint="#00000000"
    android:scrollingCache="false"/>    

</RelativeLayout>

我的线性布局:image_text_layout.xml

<?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="#00000000">

    <ImageView android:id="@+id/feed_image"
    android:layout_width="100dip" 
    android:layout_height="100dip"
    android:paddingTop="5dip"
    android:paddingRight="3dip"
    android:background="#00000000"/>

    <TextView android:id="@+id/job_text" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textColor="#FF4444"
    android:paddingTop="5dip"
    android:paddingBottom="28dip" 
    android:paddingLeft="8dip"
    android:paddingRight="8dip"
    android:background="#00000000"/>
</LinearLayout>

【问题讨论】:

    标签: java android listview selector


    【解决方案1】:

    可能有两件事。

    一:您可能需要将 LinearLayout 设为“android:clickable”或“android:focusable”。这是因为默认情况下布局不可点击。 android默认的simple_list_item在哪里。

    二:我也相信在处理自定义行时,您需要将选择器分配给自定义行的背景 (image_text_layout.xml) LinearLayout 而不是列表视图。我不太确定。

    【讨论】:

    • 我尝试了选项 1,但似乎没有任何影响,只是我使用 ListView 进行单击的能力被剥夺了。然后我尝试了选项二,看起来我遇到了充气问题,因为我的代码崩溃了。我在堆栈跟踪中找到了这个。 11-28 21:10:06.372: E/AndroidRuntime(29647): android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.LinearLayout
    猜你喜欢
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    相关资源
    最近更新 更多