【问题标题】:Creating a custom listview layout like in the Play store app像在 Play 商店应用程序中一样创建自定义列表视图布局
【发布时间】:2014-04-23 14:55:09
【问题描述】:

大家好,我想像这样创建一个列表视图项/行。我了解如何创建自定义列表视图和适配器,但获得一个每个列表项/行有点“独立”的地方我无法弄清楚。

如何去创造这样的东西?

编辑:根据 dcharms 的建议,我尝试了这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@android:color/white"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:contentDescription="@string/none"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeight"
        android:text="@string/none" />
</LinearLayout>

</RelativeLayout>

但它远不及我想要的。我只是不确定我会怎么做。任何帮助都会很棒。

【问题讨论】:

  • 您可以使用灰色的 RelativeLayout 来实现这一点,其中较小的白色布局居中。确保你也关闭了分隔线。如果你想要一个阴影,那就是白色布局下的另一个视图。
  • 如何让列表选择器在单击项目时仅突出显示较小的内部白色布局?
  • 为白色布局的背景颜色创建一个选择器。将state_pressedstate_selected 更改为蓝色。当背景应用到行的视图时,列表选择器不可见。
  • dcharms,您能否举例说明您所描述的 xml 布局的外观?
  • 好的,给我几分钟,我会用一些 xml 为你发布答案。

标签: android xml android-layout


【解决方案1】:

希望这将为您寻找所需的内容提供一个良好的起点。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainContainer"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#dddddd" >

    <View
        android:id="@+id/shadowView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:background="#cccccc" />

    <RelativeLayout
        android:id="@+id/innerContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:background="#ffffff" >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:src="@drawable/logo"
            android:contentDescription="@string/none"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/icon"
            android:text="@string/none" />

    </RelativeLayout>

</RelativeLayout>

【讨论】:

    【解决方案2】:

    在这里查看我的答案:

    Android List view layout Similar to Google play

    我已经对工作代码进行了简要说明。

    【讨论】:

    • 我相信 OP 更关心的是类似于 Google Play 商店的美学而不是功能。特别是分隔的行。
    • 也许你的权利。我只是给出了一个完整的解决方案,包括布局和工作。无论如何..感谢您的关注。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    相关资源
    最近更新 更多