Answers:
您必须使用TableLayout.LayoutParams
类似这样的东西:
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
最后一个参数是重量。
TextView tv = (TextView) findViewById(R.id.your_textview_inside_the_linearlayout); tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.1f));
TableRow.LayoutParams
应使用代替TableLayout.LayoutParams
。请参阅@Dorje的答案。
答案是必须使用TableRow.LayoutParams,而不是LinearLayout.LayoutParams或任何其他LayoutParams。
TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);
不同的LayoutParam是不可互换的,如果使用错误的LayoutParam,则似乎什么也不会发生。文本视图的父级是一个表行,因此:
http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html
LayoutParams
TableRow.LayoutParams
也为我完成了这项工作
在较早的答案中,权重传递给新的SomeLayoutType.LayoutParams对象的构造函数。在许多情况下,使用现有对象更为方便-有助于避免处理我们不感兴趣的参数。
一个例子:
// Get our View (TextView or anything) object:
View v = findViewById(R.id.our_view);
// Get params:
LinearLayout.LayoutParams loparams = (LinearLayout.LayoutParams) v.getLayoutParams();
// Set only target params:
loparams.height = 0;
loparams.weight = 1;
v.setLayoutParams(loparams);
TextView txtview = new TextView(v.getContext());
LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
txtview.setLayoutParams(params);
1f表示权重= 1;您可以指定2f或3f,视图将按照空间移动
只需在该布局中设置布局参数即可
创建参数变量
android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
1f是重量变量
将您的小部件或布局设置为
TextView text = (TextView) findViewById(R.id.text);
text.setLayoutParams(params);
LinearLayout.LayoutParams
构造函数的来源。第三个参数是重量。或文档
TextView text = new TextView(v.getContext());
text.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1f));
(要么)
TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);
1f表示权重= 1;根据您的需要,您可以给2f或3f,视图将根据空间移动。要在线性布局中的视图之间指定距离,请对“ LinearLayout” 使用weightsum。
LinearLayout ll_Outer= (LinearLayout ) view.findViewById(R.id.linearview);
LinearLayout llInner = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
llInner.Orientation = Orientation.Horizontal;
llInner.WeightSum = 2;
ll_Outer.AddView(llInner);
这对我有用,我希望它也对您有用
首先为父视图设置LayoutParams:
myTableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.FILL_PARENT));
然后为TextView设置(子级):
TableLayout.LayoutParams textViewParam = new TableLayout.LayoutParams
(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT,1f);
//-- set components margins
textViewParam.setMargins(5, 0, 5,0);
myTextView.setLayoutParams(textViewParam);
奋斗4小时后。最后,此代码对我有用。
连续有3列。
TextView serialno = new TextView(UsersActivity.this);
TextView userId = new TextView(UsersActivity.this);
TextView name = new TextView(UsersActivity.this);
serialno.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
userId.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
name.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
我在解决一个与此非常类似的解决方案时遇到了很多困难:尝试在TableRow中有两个按钮,每个按钮都是屏幕宽度的一半。无论出于何种原因,向左按钮将始终为宽度的70%,向右按钮则为宽度的30%。调用table_layout.setStretchAllColumns(true)无效,将按钮的宽度设置为屏幕的一半,也没有设置其布局权重。
我最终得到的解决方案是在TableRows中嵌套一个LinearLayout,该布局确实考虑了按钮宽度的值。
TableLayout layout = new TableLayout(this);
TableRow top_row = new TableRow(this);
left_button = styleButton();
right_button = styleButton();
LinearLayout toprow_layout = new LinearLayout (this);
toprow_layout.setOrientation(LinearLayout.HORIZONTAL);
toprow_layout.addView (left_button);
toprow_layout.addView(right_button);
toprow.addView(top_layout);
layout.addView(top_row)
private Button styleButton() {
Button btn = new Button (this);
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
btn.setWidth((int)(display.getWidth()/2)); // set width to half
btn.setHeight(((int)display.getHeight()/6)); // set height to whatevs
btn.setText("foo");
return btn;
}
layout_width=fill_parent
设置layout_weight=1
为具有have ,然后再为button1和button2设置。膨胀后,所有具有fill_parent和layout_weight = 1的元素将具有相等的大小。