Android - 如果 textView 不适合则移动它
Android - Move textView if it doesn't fit
我在 linearLayout(水平)中有 3 个 TextView,当最后一个 textview 太大或没有 space 时,它与父级匹配,如下图所示:
我想要的是,如果textview 达到linearlayout 的宽度,那么,textview 应该移动到"Property" textView 下面。 (下一行)
看看应该如何:
这是我的 XML 的片段:
<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:singleLine="true"。您的问题是您的 TextView 分布在不止一行上。
修改您的 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 以了解如何在渲染之前以编程方式查找文本视图的高度。
自 2020 年 8 月起,可以选择使用 ConstraintLayout
和虚拟布局 Flow
。
您的元素应与 Flow
元素一起包装在 ConstraintLayout
中,如以下示例 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="00.000"/>
</androidx.constraintlayout.widget.ConstraintLayout>
包含在换行中的元素(行中的所有元素如果太大则必须换行)必须有 id,然后将其添加到 Flow
的 app:constraint_referenced_ids
标签中。
app:flow_horizontalStyle="packed"
和 app:flow_horizontalBias="0.0"
是左对齐文本所必需的(flow_horizontalBias
将不起作用,除非元素被打包),但可以根据您想要的布局进行更改实现。
app:flow_wrapMode
告诉流式布局如何定位元素,并且必须设置为 chain
或 aligned
,再次需要 chain
才能能够使文本左对齐。
同样重要的是,将Flow
元素的高度设置为0dp
,这样它就可以自己决定需要多少space。
下面的TextView
应该限制在Flow
元素上,这样可以根据里面元素的位置动态定位。
Screenshot of the layout with small text
Screenshot of the layout with overflowing text
我在 linearLayout(水平)中有 3 个 TextView,当最后一个 textview 太大或没有 space 时,它与父级匹配,如下图所示:
我想要的是,如果textview 达到linearlayout 的宽度,那么,textview 应该移动到"Property" textView 下面。 (下一行)
看看应该如何:
这是我的 XML 的片段:
<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:singleLine="true"。您的问题是您的 TextView 分布在不止一行上。
修改您的 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 以了解如何在渲染之前以编程方式查找文本视图的高度。
自 2020 年 8 月起,可以选择使用 ConstraintLayout
和虚拟布局 Flow
。
您的元素应与 Flow
元素一起包装在 ConstraintLayout
中,如以下示例 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="00.000"/>
</androidx.constraintlayout.widget.ConstraintLayout>
包含在换行中的元素(行中的所有元素如果太大则必须换行)必须有 id,然后将其添加到 Flow
的 app:constraint_referenced_ids
标签中。
app:flow_horizontalStyle="packed"
和 app:flow_horizontalBias="0.0"
是左对齐文本所必需的(flow_horizontalBias
将不起作用,除非元素被打包),但可以根据您想要的布局进行更改实现。
app:flow_wrapMode
告诉流式布局如何定位元素,并且必须设置为 chain
或 aligned
,再次需要 chain
才能能够使文本左对齐。
同样重要的是,将Flow
元素的高度设置为0dp
,这样它就可以自己决定需要多少space。
下面的TextView
应该限制在Flow
元素上,这样可以根据里面元素的位置动态定位。
Screenshot of the layout with small text
Screenshot of the layout with overflowing text