【问题标题】:Custom Row for List View列表视图的自定义行
【发布时间】:2016-11-09 20:19:58
【问题描述】:

我正在尝试为我的 ListView 构建此自定义行。通过 Photoshop 的魔力,这就是我想要实现的目标:

但是当我运行我的列表视图时,它是这样出来的:

谁能帮我弄清楚我在视图中做错了什么?

这是我的 row.xml

<?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="match_parent"
    android:orientation="horizontal"
    android:padding="5dp">

    <!--  ListRow Left sied Thumbnail image -->
    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_alignParentRight="true"
            android:background="@color/COLOR_GREY" />
    </LinearLayout>

    <!-- Item Name -->
    <TextView
        android:id="@+id/txtItemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Item Name"
        android:textColor="#040404"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <!-- progress count -->
    <TextView
        android:id="@+id/txtProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbnail"
        android:layout_alignLeft="@+id/thumbnail"
        android:text="Item Name"
        android:textColor="#040404"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <!-- Retry button -->
    <Button
        android:id="@+id/btnRetry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/txtItemName"
        android:layout_toRightOf="@id/txtItemName"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:text="Retry" />

    <!-- Delete button -->
    <Button
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/btnRetry"
        android:layout_toRightOf="@id/btnRetry"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:text="Delete" />

    <!-- ProgressBar -->
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_below="@id/txtItemName"
        android:layout_alignLeft="@id/txtItemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBarItem" />

</RelativeLayout>

【问题讨论】:

  • 首先,RelativeLayout 不使用“方向”,LinearLayout` 使用,然后,当您引用视图时,您在访问其 ID 时不使用 + 符号,它仅用于创建新 ID。所以首先我会解决这个问题。并且您正在使用 ImageView 布局的全宽使其他元素出现在屏幕外

标签: java android listview material-design


【解决方案1】:

这个布局主要和你的图片一样,只需要改变按钮以使它们看起来更好。

在RelativeLayout中你不需要使用orientation 对于文本使用 sp 而不是 dip

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/row"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:baselineAligned="false"
     android:gravity="center_vertical">

<!--  ListRow Left sied Thumbnail image -->

<ImageView
    android:id="@+id/list_image"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    app:srcCompat="@mipmap/ic_launcher" />

<!-- Item Name -->
<TextView
    android:id="@+id/txtItemName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/list_image"
    android:layout_toEndOf="@+id/list_image"
    android:text="Item Name"
    android:textColor="#040404"
    android:textSize="15dip"
    android:textStyle="bold"
    android:typeface="sans" />

<!-- progress count -->
<TextView
    android:id="@+id/txtProgress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:text="100"
    android:textColor="#040404"
    android:textSize="15sp"
    android:textStyle="bold"
    android:typeface="sans" />

<!-- Retry button -->
<Button

    android:id="@+id/btnRetry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/btnDelete"
    android:layout_toStartOf="@+id/btnDelete"
    android:text="Retry" />

<!-- Delete button -->
<Button
    android:id="@+id/btnDelete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:text="Delete" />

<!--&lt;!&ndash; ProgressBar &ndash;&gt;-->
<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_below="@id/txtItemName"
    android:layout_toLeftOf="@+id/txtProgress"
    android:layout_toStartOf="@+id/txtProgress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/list_image"
    android:layout_toEndOf="@+id/list_image"
    android:progress="100"
    android:id="@+id/progressBarItem" />

您可以通过执行布局的小部分并在 stackoverflow 中搜索它们来找到如何使用布局的其他技巧,然后您可以学习如何制作更大的布局。因为之前你真的需要将一些组件包装成线性布局,但现在有了 Android Studio 2.2 你可以使用 ConstraintLayout

【讨论】:

  • 抱歉,现在使用您的布局没有任何显示。
  • 我从设计页面检查过,它可以工作。你是怎么试的?
【解决方案2】:

为什么你在 LinelarLayout 中使用 ImageView?

检查一下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <!--  ListRow Left sied Thumbnail image -->

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentLeft="true"
        android:background="@color/COLOR_GREY" />

    <!-- Item Name -->
    <TextView
        android:id="@+id/txtItemName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/list_image"
        android:layout_toRightOf="@id/list_image"
        android:text="Item Name"
        android:textColor="#040404"
        android:layout_marginLeft="10dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />

    <!-- progress count -->
    <TextView
        android:id="@+id/txtProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:layout_alignBottom="@id/list_image"
        android:text="Item Name"
        android:textColor="#040404"
        android:textSize="15sp"
        android:textStyle="bold"
        android:typeface="sans" />


    <!-- Delete button -->
    <Button
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/btnRetry"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:text="Delete" />

    <!-- Retry button -->
    <Button
        android:id="@+id/btnRetry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/txtItemName"
        android:layout_toLeftOf="@id/btnDelete"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:text="Retry" />

    <!-- ProgressBar -->
    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_alignBottom="@id/list_image"
        android:layout_toRightOf="@id/list_image"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@id/txtProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBarItem" />

</RelativeLayout>

另外,不要忘记 textSize 应该在 sp

如果您想更改按钮大小 - 只需替换:

  android:layout_width="wrap_content"
  android:layout_height="wrap_content"

并将您的自定义宽度和高度放在 dp 中,例如:

  android:layout_width="25dp"
  android:layout_height="25dp"

【讨论】:

  • 请记住,由于您选择设置固定的宽度和高度,因此您必须为不同的屏幕尺寸创建新布局
  • @AhmadAlsanie 是的,谢谢!)
  • 抱歉,现在使用您的布局没有任何显示。
  • 您是否正确关闭了相对布局标签?
  • 能否请您提供完整的课程?请和 ListView 适配器。
猜你喜欢
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 2011-07-25
  • 2023-04-06
  • 2011-06-26
  • 2016-04-30
  • 2019-10-12
  • 1970-01-01
相关资源
最近更新 更多