【问题标题】:RecyclerView listitem row are wrapping inside viewRecyclerView listitem 行包裹在视图中
【发布时间】:2016-02-26 07:48:58
【问题描述】:

布局实现

我已经实现了一个 RecyclerView 并为其设置了以下 ListItem。布局实现是我获取输出的方式。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/partspricinglistheader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*"
    android:orientation="vertical">
    <TableRow
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:weightSum="6">
        <TextView
            android:id="@+id/pp_itemno_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Item"
            android:textColor="@color/black"
            android:textSize="12sp" />
        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />
        <TextView
            android:id="@+id/pp_itemdescription_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_weight="1"
            android:text="Description"
            android:textColor="@color/black"
            android:textSize="12sp" />
        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />
        <TextView
            android:id="@+id/pp_mrp_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="MRP"
            android:textColor="@color/black"
            android:textSize="12sp" />
        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />
        <TextView
            android:id="@+id/pp_itemquantity_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Set/Quantity"
            android:textColor="@color/black"
            android:textSize="12sp" />
        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />
        <TextView
            android:id="@+id/pp_margincd_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="MARGIN"
            android:textColor="@color/black"
            android:textSize="12sp" />
        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />
        <TextView
            android:id="@+id/pp_itemcat_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="CAT"
            android:textColor="@color/black"
            android:textSize="12sp" />
    </TableRow>
</TableLayout>

我什至使用android:stretchColumns="*" 使用TableLayout,无法按照图片中的要求设置项目行。

【问题讨论】:

  • 尝试在您的TextViews 中将android:layout_width"wrap_content" 更改为 "0dp"
  • 改变 android:layout_width 和 android:layout_weight 帮助了我。

标签: android android-layout android-recyclerview android-tablelayout android-layout-weight


【解决方案1】:

在您的TextViews 中将android:layout_width"wrap_content" 更改为"0dp"

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/partspricinglistheader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:weightSum="6">
        <TextView
            android:id="@+id/pp_itemno_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Item"
            android:textColor="@color/black"
            android:textSize="12sp" />
        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />
        <TextView
            android:id="@+id/pp_itemdescription_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_weight="1"
            android:text="Description"
            android:textColor="@color/black"
            android:textSize="12sp" />
        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />
        <TextView
            android:id="@+id/pp_mrp_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="MRP"
            android:textColor="@color/black"
            android:textSize="12sp" />
        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />
        <TextView
            android:id="@+id/pp_itemquantity_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Set/Quantity"
            android:textColor="@color/black"
            android:textSize="12sp" />
        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />
        <TextView
            android:id="@+id/pp_margincd_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="MARGIN"
            android:textColor="@color/black"
            android:textSize="12sp" />
        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />
        <TextView
            android:id="@+id/pp_itemcat_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="CAT"
            android:textColor="@color/black"
            android:textSize="12sp" />
    </TableRow>
</TableLayout>

【讨论】:

  • 这不起作用,我为每个 TextView 分别使用了静态重量和高度。
  • 好的,所以将您的解决方案发布给未来的用户。
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:stretchColumns="6">

    <TableRow
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <TextView
            android:id="@+id/pp_itemno_tv"
            android:layout_width="0.5dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.1"
            android:text="Item"
            android:textColor="@color/black"
            android:textSize="12sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />

        <TextView
            android:id="@+id/pp_itemdescription_tv"
            android:layout_width="0.5dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:gravity="left"
            android:text="Description"
            android:textColor="@color/black"
            android:textSize="12sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />

        <TextView
            android:id="@+id/pp_mrp_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:text="MRP"
            android:textColor="@color/black"
            android:textSize="12sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />

        <TextView
            android:id="@+id/pp_itemquantity_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Set/Quantity"
            android:textColor="@color/black"
            android:textSize="12sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />

        <TextView
            android:id="@+id/pp_margincd_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.4"
            android:gravity="center"
            android:text="MARGIN"
            android:textColor="@color/black"
            android:textSize="12sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="38dp"
            android:background="@color/white" />

        <TextView
            android:id="@+id/pp_itemcat_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="CAT"
            android:textColor="@color/black"
            android:textSize="12sp" />

    </TableRow>
</TableLayout>

【讨论】:

    猜你喜欢
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2016-09-05
    相关资源
    最近更新 更多