Java 列表视图的自定义行

Java 列表视图的自定义行,java,android,listview,material-design,Java,Android,Listview,Material Design,我正在尝试为我的ListView构建此自定义行。通过Photoshop的魔力,我正试图实现以下目标: 但当我运行列表视图时,结果如下所示: 有人能帮我找出我在视图上做错了什么吗 这是我的row.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width

我正在尝试为我的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>

为什么在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>
并在dp中输入自定义宽度和高度,如:


这是布局,主要是做你的图片,只需要改变按钮,以使他们看起来更好

在RelativeLayout中,您不需要使用方向 对于文本,使用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

首先,
RelativeLayout
不使用“方向”,LinearLayout`使用,然后,当你引用一个视图时,在访问其ID时不使用
+
符号,它只用于创建一个新的ID。所以首先我要解决这个问题。您使用的是布局的全宽,而ImageView使其他元素显示在屏幕外,请记住,您必须为不同的屏幕大小创建一个新布局,因为您选择了设置固定的宽度和宽度height@AhmadAlsanie是的,谢谢!)很抱歉,使用您的布局时,现在没有显示任何内容。您是否正确关闭了相对布局标记?你能给我提供你全部的课程吗?和ListView适配器。对不起,使用您的布局,现在什么都没有显示。我已经从设计页面检查过了,它可以工作了。你怎么试的?
  android:layout_width="25dp"
  android:layout_height="25dp"
<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" />