- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一个 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_column
和 android:layout_span
属性来准确定义一个项目应该在哪一列,以及它应该占据多少列。另请注意,TableLayout
和 TableRow
基本上忽略了应用的任何布局参数并使用非常具体(已记录)的参数,因此将它们添加到您的代码中只会混淆实际发生的事情.当然,这一切都可以通过编程方式构建,也可以在 XML 中构建。
如果这不能提供您需要的灵 active ,那么我会建议创建您自己的自定义布局,该布局可以扩展或基于 LinearLayout
。该机制用于测量体重的 child 并不那么复杂,然后您可以覆盖每个 child 在测量后的放置方式。这是一个source link LinearLayout
如果你想看看平台是如何做这些事情的。
关于android - 替代 LinearLayouts 的 layout_weight 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13560356/
这个。如果最后一个布局有 .6 (60%),我不明白为什么第一个布局看起来比第二个布局大。 最佳答案 @Miguel Barra 适用于此 andr
也许我误解了 layout_weight 参数,但我不明白为什么这样的东西不起作用...... 我在 LinearLayout 中有三个或更多 TextView 。我想将它们放在具有百分比
我研究了另一个 stackoverflow 问题//答案,发现 layout_weight 的工作定义是“此属性将“重要性”值分配给 View ,并允许它扩展以填充父 View 中的任何剩余空间”(来
我有 3 张图片的布局。我给他们每个人分配了权重来控制他们分配的宽度。 问题是权重使图像的所有宽度可点击,当我希望可点击区域只有图片时,如何实现? 这是布局的相关部分:
我的应用应该是这样的: 这应该是它的外观,尽管我之前使用它的方式并不能很好地缩放。所以这就是我这样做的原因,但它给我带来了很多问题。 但即使是看似相同的设置,第二排也会占据整个房间,看起来像这样: 有
我正在尝试实现一个将分为 2 个子布局的布局。我希望左侧布局占屏幕的 1/4,而右侧布局占屏幕的剩余 3/4。我的目标如下:如果用户按下按钮,则左侧布局将被隐藏,右侧布局将占据整个屏幕(不确定这是否可
我想要一个 TableRow 中的 TextView 和 EditText,并且我希望 EditText 的宽度比 TextView 大两倍,所以我把它放在 TextView 区域: android:
我读过很多问题,说问题出在 layout_width = 0dip 但我试过了,但无法实现。 我有这个持有人: 还有这个部分: 但我的 View 按钮显示仍然显示在左侧......有没有一种方
在我的应用程序中,我有一个如图所示的 Activity (图 #1)。在 TableLayout 中,我将构建一个包含 n 行和 6 列的表。我构建表格插入,就像列表一样,n LinearLayout
在下面的布局 xml 中,我创建了一个带有两个 subview 的水平 LinearLayout。 LinearLayout 子项应该动态拉伸(stretch)其宽度,ImageView 的宽度取决于
基本上我有一个线性布局中的 ImageView,当我的每个布局上设置的权重被调整大小时,布局中的 ImageView 也是如此。问题是 - 只有实际改变宽度的高度保持不变。 为什么会出现问题?我有 c
在代码版本 1 中按钮1的高度是按钮2的5倍 在代码版本 2 中按钮2的高度是按钮1的5倍 当 android:
我正在尝试制作一个计算器应用程序。我有一个外部线性布局,它是垂直方向,然后有嵌套的线性布局,它具有水平方向,我的按钮将在其中放置。只有最后一个嵌套的线性布局没有正确调整按钮的宽度,因为我希望“0”按钮
免责声明:AFAIK 这个问题目前还没有针对 XAMARIN ANDROID 的答案。 Android/Java已多次回答. 我的问题是我没有将 layout_weight 作为参数的 GridLay
为什么下面的 list 只显示第二个 TextView(红色)? 我知道如果我设置 android:layout_height="0px" 它只会显示第一个 TextView(
我正在处理一个包含 5 个按钮的 fragment 。当它在像平板电脑这样的大屏幕上时,我使用一个将所有 5 个按钮放在顶部的 fragment 。我将它们的所有 layout_width 设置为 1
我正在尝试使用 layout_weight 构建一个 android 布局以适应所有尺寸的设备,但我在理解它的行为时遇到了一些困难。 我注意到更改 layout_width/layout_height
当我以编程方式设置 TableRow layout_weight 时遇到问题。以下代码是在 table_body 元素中扩展的 TableLayout: 我只想在 TableLayout 中显示
我有一个简单的问题:如何以编程方式获取 layout_weight?我看到很多题目,有些Layoute设置layout_weight的时候,但是怎么获取呢? 最佳答案 获取LinearLayout.L
在下面的 xml 中,我试图绘制一个包含两个 block (一个 LinearLayout 和一个 TextView)的布局。我希望 LinearLayout 比 TextView 大 5 倍。此 x
我是一名优秀的程序员,十分优秀!