gpt4 book ai didi

android - 仅当空间足够时才在右侧查看,否则在底部

转载 作者:太空狗 更新时间:2023-10-29 16:28:40 25 4
gpt4 key购买 nike

使用 ConstraintLayout 我有 2 个 TextView,当它们有足够的空间来查看两者的内容时,我希望它们彼此相邻。但是,如果 TextView A 占用大量空间并填满所有屏幕宽度,那么我希望 TextView B 位于 TextView A 下方,而不是仍在右侧但不可见(在屏幕外)。

因此,使用下面的代码,我将 2 个 TextView 放在了一起。

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/text_view_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
tools:text="a very long text ..." />

<TextView
android:id="@+id/text_view_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toRightOf="@+id/text_view_a"
app:layout_constraintTop_toTopOf="parent"
tools:text="some text" />

</android.support.constraint.ConstraintLayout>

如果 TextView B 没有任何空间留给他,我应该如何编辑此代码 fragment ,以便 TextView B 位于 TextView A 之下?

最佳答案

看看 Google 的 FlexboxLayout。它可能是您流动 TextView 所需要的。您可以找到一些文档 here和教程 here .

FlexboxLayout is a library project which brings the similar capabilities of CSS Flexible Box Layout Module to Android.

您可以考虑将 TextView 包装在 ConstraintLayout 内的 FlexbotLayout 中,以允许它们根据需要流动。

这是一个示例布局。由于最上面的TextView文本比较长,所以两个TextView会叠在一起。如果缩短此文本,两个 TextView 将并排显示。

希望对您有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.flexer.MainActivity">

<com.google.android.flexbox.FlexboxLayout 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="0dp"
app:alignContent="stretch"
app:alignItems="stretch"
app:flexWrap="wrap"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/text_view_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="a very long text ...a very long text ...a very long text ...a very long text ...a very long text ...a very long text ...a very long text ...a very long text ..." />

<TextView
android:id="@+id/text_view_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="some text" />
</com.google.android.flexbox.FlexboxLayout>
</android.support.constraint.ConstraintLayout>

关于android - 仅当空间足够时才在右侧查看,否则在底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44258042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com