gpt4 book ai didi

android - 具有多个子级的水平 LinearLayout,当没有更多水平空间时,将子级移动到新行下方

转载 作者:可可西里 更新时间:2023-11-01 19:05:18 25 4
gpt4 key购买 nike

我想要一个水平的LinearLayout它和屏幕一样宽,和它的 child 一样高,但诀窍是它的 child 每个都有动态宽度,我不希望他们离开屏幕(剪下)。我希望它们在新行中流动/中断,以便它们都可见。

虽然与 Android 完全无关,但它的工作方式应该类似于内联 <div> 的工作方式以 HTML 格式工作。

这是我现在拥有的:

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="If you would enter some digits in this field " />
<EditText
android:id="@+id/tvDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="enter some digits here"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" that would be great" />
</LinearLayout>

但是一旦LinearLayout的 children 变得比屏幕更宽,多余的部分离开屏幕/不可见。

最佳答案

I would like to have a horizontal LinearLayout that is as wide as the screen and as high as its children, but the trick is that its children will have dynamic width each and I don't want them going off screen (cut out).

LinearLayout 不能做到这一点(SDK 中的任何默认布局都不能做到这一点)因为 LinearLayout 被设置为放置 all 一个水平或垂直行中的 child 。此外,任何类型的基于尺寸的条件布局规则尚不可用(例如在您的情况下 LinearLayout 的可用宽度)无论如何在 xml 中都是不可能的。

您需要的是一个自定义布局,它衡量子级使用可用空间在下面的新行上移动任何不合适的子级(所谓的 FlowLayout)。

编辑:

Google 现在提供 flexbox在 Android 上实现 web 的 flexbox 布局的库。使用该库,FlexboxLayout 的子项将根据它们的宽度放置在多行上,方法是使用 flexDirection(值为 row ) 和 flexWrap(值为 wrap)属性。

关于android - 具有多个子级的水平 LinearLayout,当没有更多水平空间时,将子级移动到新行下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18413641/

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