首页 > 解决方案 > 如何使用屏幕限制调整文本视图?

问题描述

我有一个卡片视图,我在其中显示成分列表,但我面临的问题是有时成分名称变得太长,以至于它会将文本视图和旁边的按钮踢出屏幕。即使成分名称变长,如何调整屏幕内的整个卡片视图?

那是我的 ingredients_list.xml 的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    app:cardCornerRadius="12dp"
    app:cardElevation="5dp"
    android:backgroundTint="#333333"
    android:layout_margin="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#333333"
        android:layout_margin="4dp"
        android:padding="5dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/ith_ingredient_inventory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ingredient i"
            android:gravity="center"
            android:textColor="@color/colorAccent"
            android:textSize="20dp"
            android:textStyle="bold"
            android:padding="10dp"
            />
        <TextView
            android:id="@+id/ith_ingredient_quantity_inventory"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@drawable/round_corners"
            android:backgroundTint="@color/colorAccent"
            android:text="100"
            android:textSize="20dp"
            android:textAlignment="center"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:padding="5dp"
            android:layout_margin="5dp">

        </TextView>
        <TextView
            android:id="@+id/ith_ingredient_quantity_unit_inventory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="unit"
            android:textColor="@color/colorAccent"
            android:textSize="16dp"
            android:padding="10dp"
            />
        <Button
            android:id="@+id/add_quantity_inventory"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/plus"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:layout_marginLeft="10dp">
        </Button>
    </LinearLayout>
 </androidx.cardview.widget.CardView>

标签: androidandroid-layoutandroid-recyclerviewtextviewandroid-cardview

解决方案


我将您的 LinearLayout 更改为 ConstraintLayout 并进行了一些其他更改:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:layout_margin="10dp"
    android:backgroundTint="#333333"
    app:cardCornerRadius="12dp"
    app:cardElevation="5dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:background="#333333"
        android:padding="5dp">

        <TextView
            android:id="@+id/ith_ingredient_inventory"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:gravity="center"
            android:maxLines="1"
            android:text="ingredient i ingredient i ingredient i ingredient i ingredient i"
            android:textColor="@color/colorAccent"
            android:textSize="20dp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/ith_ingredient_quantity_inventory"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/ith_ingredient_quantity_inventory"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="5dp"
            android:backgroundTint="@color/colorAccent"
            android:text="100"
            android:textAlignment="center"
            android:textColor="@android:color/black"
            android:textSize="20dp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/ith_ingredient_quantity_unit_inventory"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/ith_ingredient_quantity_unit_inventory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="unit"
            android:textColor="@color/colorAccent"
            android:textSize="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/add_quantity_inventory"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/add_quantity_inventory"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_margin="10dp"
            android:foreground="?attr/selectableItemBackgroundBorderless"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"></Button>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

这是结果:

在此处输入图像描述

因此,您可以将 ConstraintLayout 与我的属性一起使用,并且您不想覆盖其他视图的 TextView 必须具有 maxLine 值和 ellipsize 以在最后显示三个点,当它很长时。


推荐阅读