以编程方式设置 TextView 的布局权重
我正在尝试动态创建 TableRow
对象并将它们添加到 TableLayout
中。 TableRow
对象有 2 个项目,一个 TextView
和一个 CheckBox
。 TextView
项需要将其布局权重设置为 1,才能将 CheckBox
项推到最右侧。
我找不到有关如何以编程方式设置 TextView
项的布局权重的文档。
I'm trying to dynamically create TableRow
objects and add them to a TableLayout
.
The TableRow
objects has 2 items, a TextView
and a CheckBox
. The TextView
items need to have their layout weight set to 1 to push the CheckBox
items to the far right.
I can't find documentation on how to programmatically set the layout weight of a TextView
item.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您必须使用
TableLayout.LayoutParams
,如下所示:最后一个参数是权重。
You have to use
TableLayout.LayoutParams
with something like this:The last parameter is the weight.
答案是您必须使用 TableRow.LayoutParams,而不是 LinearLayout.LayoutParams 或任何其他 LayoutParams。
不同的 LayoutParams 是不可互换的,如果你使用了错误的布局参数,那么似乎什么也不会发生。文本视图的父级是表行,因此:
http://developer. android.com/reference/android/widget/TableRow.LayoutParams.html
The answer is that you have to use TableRow.LayoutParams, not LinearLayout.LayoutParams or any other LayoutParams.
The different LayoutParams are not interchangeable and if you use the wrong one then nothing seems to happen. The text view's parent is a table row, hence:
http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html
在前面的答案中,权重被传递给新的 SomeLayoutType.LayoutParams 对象的构造函数。
尽管如此,在许多情况下,使用现有对象会更方便 - 它有助于避免处理我们不感兴趣的参数。
例如:
In the earlier answers weight is passed to the constructor of a new SomeLayoutType.LayoutParams object.
Still in many cases it's more convenient to use existing objects - it helps to avoid dealing with parameters we are not interested in.
An example:
1f表示weight=1;您可以指定 2f 或 3f,视图将根据空间移动
1f is denotes as weight=1; you can give 2f or 3f, views will move accoding to the space
只需在该布局中设置布局参数,例如
创建参数变量
1f 是权重变量,
设置您的小部件或布局,例如
just set layout params in that layout like
create param variable
1f is weight variable
set your widget or layout like
(OR)
1f 称为权重=1;根据您的需要,您可以指定 2f 或 3f,视图将根据空间移动。要在线性布局中指定视图之间的距离,请使用“LinearLayout”的weightsum。
(OR)
1f is refered as weight=1; according to your need you can give 2f or 3f, views will move accoding to the space. For making specified distance between views in Linear layout use weightsum for "LinearLayout".
这应该对你有用
This should works to you
你也可以像这样单独给出重量,
You can also give weight separately like this ,
这对我有用,我希望它也对您有用
首先为父视图设置 LayoutParams:
然后为 TextView(子视图)设置:
This work for me, and I hope it will work for you also
Set the LayoutParams for the parent view first:
then set for the TextView (child):
经过4个小时的奋战。
最后,这段代码对我有用。
3 列排成一行。
After strugling for 4 hours.
Finally, This code worked for me.
3 Columns are there in a row.
还有另一种方法可以做到这一点。如果您只需要设置一个参数,例如“高度”:
There is another way to do this. In case you need to set only one parameter, for example 'height':
我在解决与此非常相似的解决方案时遇到了相当多的困难:尝试在 TableRow 中放置两个按钮,每个按钮都是屏幕宽度的一半。无论出于何种原因,左侧按钮始终约为宽度的 70%,右侧按钮约为 30%。调用 table_layout.setStretchAllColumns(true) 没有任何效果,将按钮的宽度设置为屏幕的一半也没有效果,也没有设置其布局权重。
我最终得到的解决方案是在 TableRows 中嵌套一个 LinearLayout,它确实考虑了按钮宽度的值。
I had a fair amount of difficulty with a solution something very similar to this: trying to have two buttons in a TableRow, with each being half the screen width. For whatever reason, the left button would always be about 70% of the width, and the right button 30%. Calling table_layout.setStretchAllColumns(true) had no effect, nor did setting the button's width to half the screen, nor setting their layout weight.
The solution I ended up with was nesting a LinearLayout in the TableRows, which did take into account the value of the buttons' width.