首页 > 解决方案 > EditText 在 RecyclerView 的 ViewHolder 中被切断

问题描述

我在 Activity 的 LinearLayout 内的 RecyclerView 内有一个自定义 ViewHolder。我试图让 ViewHolder 中的 EditText 显示其中的所有文本。它不断地从视图的其余部分中被剪掉:

在此处输入图像描述

这是相关的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">
    <CheckBox
        android:id="@+id/add_existing_creature_add_this_creature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"/>

    <!-- Name of the creature -->
    <TextView
        android:id="@+id/add_existing_creature_list_item_creature_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:textAlignment="center"
        android:textSize="24sp"/>

    <!-- Copies of the creature -->
    <LinearLayout
        android:id="@+id/existing_creature_list_item_copies_group"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:visibility="visible"
        android:padding="4dp" >
        <TextView
            android:labelFor="@+id/add_existing_creature_number_of_copies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/x"/>
        <EditText
            android:id="@id/add_existing_creature_number_of_copies"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:layout_gravity="bottom"
            android:gravity="bottom"
            android:padding="4dp"/>
    </LinearLayout>
    ....
    <!-- More XML enter code here--->

如何让剪辑停止?

标签: android

解决方案


您的布局(existing_creature_list_item_copies_group)的高度受限于父LinearLayout(sec线性)测试下面的代码,我通过填充2dp文本视图(add_existing_creature_list_item_creature_name)和固定子重力布局解决了这个问题:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom"
    android:paddingTop="8dp"
    android:paddingBottom="8dp">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <CheckBox
            android:id="@+id/add_existing_creature_add_this_creature"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"/>

        <!-- Name of the creature -->
        <TextView
            android:id="@+id/add_existing_creature_list_item_creature_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:text="5"
            android:padding="2dp"
            android:textAlignment="center"
            android:textSize="24sp"/>

        <!-- Copies of the creature -->
        <LinearLayout
            android:id="@+id/existing_creature_list_item_copies_group"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:visibility="visible"
            android:layout_gravity="center"
            android:gravity="center"
            >
            <TextView
                android:labelFor="@+id/add_existing_creature_number_of_copies"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="x"/>
            <EditText
                android:id="@id/add_existing_creature_number_of_copies"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:layout_gravity="bottom"
                android:gravity="bottom"
                android:text="2"
                android:padding="8dp"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

推荐阅读