gpt4 book ai didi

android - 替代 LinearLayouts 的 layout_weight 属性?

转载 作者:行者123 更新时间:2023-11-29 14:52:41 25 4
gpt4 key购买 nike

我需要一个 View 来容纳多个 TextView,但遗憾的是我不知道确切的数目。我需要 TextViews 对堆栈进行排序,或者在彼此下面或彼此紧挨着(想象俄罗斯方 block ,但只有矩形或正方形)。我尝试考虑的方式是使用 LinearLayout 水平或垂直放置它们。如果它们彼此相邻,我然后使用它们的重量适本地拉伸(stretch)它们。否则,我使用垂直方向。问题在于具有更复杂堆栈的嵌套 LinearLayouts 的性能。我考虑过使用 RelativeLayout,但这行不通,因为我需要 TextViews 不重叠。所以就像如果他们彼此相邻,他们需要每个人均匀地占据足够的空间。使用 layout_weight 效果很好。我希望有人知道如何使这项工作正确/替代。

这是一个给我警告的例子(只是我正在以编程方式做的一个简单例子):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello World 1"
android:background="#000000"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello World 3"
android:background="#000000"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello World 4"
android:background="#000000"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World 2"
android:background="#000000" />
</LinearLayout>

顺便说一下,我已经用 RelativeLayout 试过了,但是其中一个 TextView 需要设置大小,但我需要它们均匀占用空间。我不知 Prop 体尺寸。测量屏幕宽度并以这种方式划分它既笨重又不精确。我感谢任何人的意见。

编辑: 所以我一直在考虑更多,并想到了使用 RelativeLayout 的解决方案。 Android docs 中有一个很好的示例,但唯一的问题是其中一个 View 需要设置大小,以便另一个可以拉伸(stretch)。但是,如果有一种方法可以让它们都没有固定尺寸,那也可以,这样它们就可以拉伸(stretch)了。有人曾尝试这样做吗?

最佳答案

有点不清楚您的最终用例是什么样的,所以我不知道这是否能完全解决您的问题,但您可以看看 TableLayout ( docs link )。它基于 LinearLayout,因此项目仍然可以定义为均匀占据给定空间,但它允许您根据行和列定义位置和跨度。

例如,您的示例代码如下所示:

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<!-- Wrap all items in a given row in a TableRow -->
<TableRow>
<TextView
android:text="Hello World 1"
android:background="#000000"/>
<TextView
android:text="Hello World 3"
android:background="#000000"/>
<TextView
android:text="Hello World 4"
android:background="#000000"/>
</TableRow>
<!-- If a child occupies an entire row, it can be by itself -->
<TextView
android:text="Hello World 2"
android:background="#000000"/>
</TableLayout>

您还可以使用 android:layout_columnandroid:layout_span 属性来准确定义一个项目应该在哪一列,以及它应该占据多少列。另请注意,TableLayoutTableRow 基本上忽略了应用的任何布局参数并使用非常具体(已记录)的参数,因此将它们添加到您的代码中只会混淆实际发生的事情.当然,这一切都可以通过编程方式构建,也可以在 XML 中构建。

如果这不能提供您需要的灵 active ,那么我会建议创建您自己的自定义布局,该布局可以扩展或基于 LinearLayout。该机制用于测量体重的 child 并不那么复杂,然后您可以覆盖每个 child 在测量后的放置方式。这是一个source link LinearLayout 如果你想看看平台是如何做这些事情的。

关于android - 替代 LinearLayouts 的 layout_weight 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13560356/

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