【问题标题】:Android - Move textView if it doesn't fitAndroid - 如果不适合则移动 textView
【发布时间】:2016-06-27 14:54:43
【问题描述】:

我在一个线性布局(水平)中有 3 个 TextView,当最后一个 textview 太大或没有空间时,它与父级匹配,如下图所示:

Image Here

我想要的是,如果文本视图达到线性布局的宽度,那么,文本视图应该移动到“属性”文本视图下方。 (下一行)

看看它应该是怎样的:

Image here

这是我的 XML 的 sn-p:

  <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="Property"
                    android:id="@+id/txtPropertyType" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="/ 127 m"
                    android:id="@+id/txtArea"
                    android:layout_marginLeft="10dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="/ 3 roomsssssssssdddd"
                    android:singleLine="false"
                    android:id="@+id/txtNumRoom"
                    android:layout_marginLeft="5dp" />
            </LinearLayout>

在此先感谢 x)

【问题讨论】:

    标签: android textview width


    【解决方案1】:

    修改你的xml并在LinearLayout下面添加另一个TextView:

            <LinearLayout
    
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="Property"
                    android:id="@+id/txtPropertyType" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="/ 127 m"
                    android:id="@+id/txtArea"
                    android:layout_marginLeft="10dp" />
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearance"
                    android:text="/ 3 roomsssssssssdddd"
                    android:singleLine="false"
                    android:id="@+id/txtNumRoom"
                    android:layout_marginLeft="5dp" />
            </LinearLayout>
    
            <TextView
                android:visibility="gone"
                android:layout_below="@+id/linearLayout"
                android:id="@+id/long_room"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="dfgdfdfgddfgdfgdd"/>
    

    现在,您有 2 个选择:

    选项1:检查房间号文本视图的长度。如果它超过了,比如说,12(你必须找到所有字母都是最宽的数字——比如w),然后让txtNumRoom GONE 和long_room 的可见性可见。

    选项 2:txtNumRoom 被渲染和显示之前找到它的高度。如果这个高度大于txtArea 的高度,这意味着文本视图已经多行了。同样,在这种情况下,使 txtNumRoom GONE 的可见性和 long_room 的可见性可见。 请参阅this question 了解如何在呈现之前以编程方式查找文本视图的高度。

    【讨论】:

    • 成功了!我需要做一个 adapter.notifyDataSetChanged();在我的 listView 中,但它有效:D 非常感谢!
    【解决方案2】:

    只需在每个文本视图上添加 android:singleLine="true" 即可。您的问题是您的 TextView 分布在不止一行上。

    【讨论】:

    • 如果我将singleLine设置为true,当它不适合时它总是会变成椭圆形..我不想要那个。
    【解决方案3】:

    自 2020 年 8 月起,可以选择使用 ConstraintLayout 和虚拟布局 Flow

    您的元素应包含在 ConstraintLayoutFlow 元素中,如以下示例 XML:

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearance"
            android:text="Property"
            android:id="@+id/txtPropertyType"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearance"
            android:text="/ 127 m"
            android:id="@+id/txtArea"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearance"
            android:text="/ 3 roomssssssssssssssssssssss"
            android:singleLine="false"
            android:id="@+id/txtNumRoom"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
    
        <androidx.constraintlayout.helper.widget.Flow
            android:id="@+id/flow"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:constraint_referenced_ids="txtPropertyType, txtArea, txtNumRoom"
            app:flow_horizontalGap="4dp"
            app:flow_horizontalStyle="packed"
            app:flow_horizontalBias="0.0"
            app:flow_wrapMode="chain"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/flow"
            app:layout_constraintStart_toStartOf="parent"
            android:textAppearance="?android:attr/textAppearance"
            android:text="$2000.000"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    包装中涉及的元素(行中的所有元素如果太大则必须包装)必须具有 ID,然后将其添加到 Flowapp:constraint_referenced_ids 标记中。

    app:flow_horizontalStyle="packed"app:flow_horizontalBias="0.0" 是左对齐文本所必需的(flow_horizontalBias 将不起作用,除非元素被打包),但可以根据您想要实现的布局进行更改。

    app:flow_wrapMode 告诉流布局如何定位元素,并且必须设置为chainaligned,同样chain 是能够左对齐文本所必需的。

    Flow 元素的高度设置为0dp 也很重要,这样它就可以自行确定需要多少空间。

    下面的TextView 应该被限制在Flow 元素上,这样它就可以根据里面元素的位置动态定位。

    Screenshot of the layout with small text

    Screenshot of the layout with overflowing text

    【讨论】:

      猜你喜欢
      • 2013-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      相关资源
      最近更新 更多