java - ListView 的自定义行

标签 java android listview material-design

我正在尝试为我的 ListView 构建这个自定义行。通过 Photoshop 的魔力,我正在努力实现以下目标:

enter image description here

但是当我运行我的 ListView 时,结果是这样的:

enter image description here

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

这是我的 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 中你不需要使用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

关于java - ListView 的自定义行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40515328/

相关文章:

java - 如何在破解原始 jar 文件后重新创建 jar 文件?

android - toast "sending message to a Handler on a dead thread"

java - 我的 ListView 不显示我的数据,但显示我的包名称

c# - InvalidArgument= '0' 的值对于 'index' 无效。参数名称: index

java - JPA 乐观锁定规范是否支持根据客户端提供的版本进行验证

java - OSGi 中的回调和传递对象引用

java - 从java中的jtable单元格获取最新值

javascript - 动态创建的音频对象无法在 Android Chrome 浏览器中播放

java - 创建 ListNode 的新实例

c# - 正确调整 wpf ListView 最后一列的大小