我需要一个View来保存一些TextView,并且不幸的是我不知道确切的数字。我需要将TextViews堆叠在彼此之下或彼此相邻(想象俄罗斯方块,但只有矩形或正方形)。我试过的方法是使用LinearLayout水平或垂直地保持它们。然后,如果它们彼此相邻,我会使用它们的重量来适当地拉伸它们。否则,我使用垂直方向。问题是嵌套的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需要设置一个大小,但是我需要它们来均匀占用空间。我不知道具体的尺寸。测量屏幕宽度并将其分开也既笨重又不精确。我感谢任何人的输入。
编辑:所以我一直在考虑更多,并考虑使用RelativeLayout的解决方案。在Android docs中有一个很好的例子,但唯一的问题是其中一个视图需要是一个设置大小,所以另一个可以伸展。但如果有一种方法可以让所有人都没有设定尺寸,那也可以起作用,那么他们就可以伸展。有人试过这样做吗?
答案 0 :(得分:3)
有点不清楚您的最终用例是什么样的,所以我不知道这是否能彻底解决您的问题,但您可以查看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_column
和android:layout_span
属性来准确定义项目所在的列,以及应占用的列数。另请注意,TableLayout
和TableRow
基本上忽略了所应用的任何布局参数,并使用非常具体(记录的)参数,因此将它们添加到代码中只会混淆实际发生的事情。当然,这可以通过编程和XML格式构建。
如果这不能提供您所需的灵活性,我建议您创建自己的自定义布局,该布局可以扩展或基于LinearLayout
。该机制用于测量体重并不复杂的儿童,然后您可以覆盖测量后每个儿童的放置方式。如果您想了解平台如何执行这些操作,请source link到LinearLayout
。
答案 1 :(得分:3)
更新:正如我们所知,支持库百分比已从API级别26弃用。ConstraintLayout
是实现相同平面xml结构的新方法。
更新了样本
<android.support.constraint.ConstraintLayout
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="match_parent">
<TextView
android:id="@+id/fifty_thirty"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ffff8800"
android:gravity="center"
android:text="@string/fifty_fifty_text"
android:textColor="@android:color/white"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.5"
android:textSize="25sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ffff5566"
android:gravity="center"
android:text="@string/fifty_fifty_text"
android:textColor="@android:color/white"
android:textSize="25sp"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.5"
app:layout_constraintLeft_toRightOf="@id/fifty_thirty"
app:layout_constraintTop_toBottomOf="@id/fifty_thirty"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5" />
</android.support.constraint.ConstraintLayout>
<强>已过时强>
更新:我们可以使用Android支持库。
compile 'com.android.support:percent:23.0.0'
考虑这个演示,将屏幕划分为50-50%。
<android.support.percent.PercentRelativeLayout
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="match_parent">
<TextView
android:id="@+id/fifty_huntv"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff7acfff"
android:text="20% - 50%"
android:textColor="@android:color/white"
app:layout_heightPercent="20%"
app:layout_widthPercent="50%" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="@id/fifty_huntv"
android:background="#ffff5566"
android:text="80%-50%"
app:layout_heightPercent="80%"
app:layout_widthPercent="50%"
/>
</android.support.percent.PercentRelativeLayout>
<强>输出:强>