【问题标题】:Dynamically Changing List Row Background When Selected选择时动态更改列表行背景
【发布时间】:2014-02-02 20:03:43
【问题描述】:

我在网上搜索了解决此问题的方法,并尝试了不同的方法,但没有找到答案。我有一个列表视图和列表行的自定义行布局。在我的列表行 xml 文件中,有一个带有 ImageView 和文本的相对布局。除此之外,我将这个相对布局的背景设置为某种形状,这非常棒。现在在运行时,我想更改此列表行背景以显示向内压(我在想只是使填充更小或其他东西)。我尝试为压制形状制作另一个 xml 文件,但没有奏效。我还尝试通过执行以下操作来编写代码:

RelativeLayout rl = (RelativeLayout)view.findViewById(R.id.relativerow);
                    rl.setBackgroundResource(R.drawable.item_shape_pressed);

我猜我必须制作自己的 onPressed 方法才能使其正常工作? 这是我为每个列表行背景使用的形状 xml 文件:

 <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_shape">
    <padding android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
    <corners android:radius="20dp" />
    <solid android:color="#FFF" />
</shape>

这是列表视图中每一行的 XML。看看我把背景属性放在顶部的位置。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativerow"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/item_shape"
    android:padding="5dip" >


    <LinearLayout android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="3dip"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="60dip"
            android:layout_height="60dip"/>

    </LinearLayout>


    <TextView
        android:id="@+id/articletitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="16sp"
        android:textStyle="bold"/>


    <TextView
        android:id="@+id/articldesc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/articletitle"
        android:textColor="#343434"
        android:textSize="13sp"
        android:textStyle="italic"
        android:layout_marginTop="1dip"
        android:layout_toRightOf="@+id/thumbnail"
   />


</RelativeLayout>

那么,在运行时我有什么办法可以将这个相对视图中的背景从正常的 item_shape 更改为另一个按下的 item 形状?如果有人能告诉我如何做到这一点,我当然会很感激。这只是最近困扰我的应用程序的一小部分,但如果让应用程序看起来更完整,那将是一个不错的选择。

【问题讨论】:

  • 正确设置listSelector。相关问题可以找herehere
  • 向我们展示您的 ListAdapter

标签: android xml listview android-relativelayout


【解决方案1】:

尝试为 ListView 创建您自己的选择器,例如 res/drawable 文件夹中的 listview_item_selector.xml 并将此选择器添加到您的 RelativeLayout 的背景中。

以下代码将是:listview_item_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/item_shape_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/item_shape_focused" android:state_focused="true" />
    <item android:drawable="@drawable/item_shape" />
</selector>

在你的 RelativeLayout 中放:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativerow"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/listview_item_selector"
    android:padding="5dip" >

    .....

【讨论】:

  • 很高兴为您服务
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 2014-01-30
  • 2023-01-01
  • 2015-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多