Android应用程序->;android:Java中的布局和权重
我已经在互联网上和这里寻找过了,但不知怎么的,没有一个解决方案对我有效。。 我想用Java实现这一点(这是XML代码):Android应用程序->;android:Java中的布局和权重,java,android,xml,Java,Android,Xml,我已经在互联网上和这里寻找过了,但不知怎么的,没有一个解决方案对我有效。。 我想用Java实现这一点(这是XML代码): 请删除代码“row.setWeightSum(1.0f);”中的这一行,然后再试一次,它将起作用。“不起作用”是什么意思?另外,在行中,setLayoutParams(新的LayoutParams…您应该使用您的表行所在视图的LayoutParams。@balajikoduri仍然没有work@dst如果不工作,我的意思是,我得到了所有的编辑文本,但它们都设置为“包装内容”…
请删除代码“row.setWeightSum(1.0f);”中的这一行,然后再试一次,它将起作用。“不起作用”是什么意思?另外,在
行中,setLayoutParams(新的LayoutParams…
您应该使用您的表行所在视图的LayoutParams
。@balajikoduri仍然没有work@dst如果不工作,我的意思是,我得到了所有的编辑文本,但它们都设置为“包装内容”…我不太明白你的意思,但我试着使用“row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));”如果这就是你的意思,那么从查看代码的角度来看,仍然不起作用我猜我的操作TableLayout.LayoutParams
。你没有发布与此相关的部分;但是我不确定这是否与问题有关(更像是一个文体注释,以防止bug)。您尝试过XML吗?从我阅读的文档中可以看出,TableRow
(当放置在TableLayout
中时)应该覆盖布局参数为“匹配父项”和WRAP\u内容
,无论您设置了什么。为什么不使用列?您阅读了吗?
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editText1"
android:layout_width="0px"
android:layout_height="wrap_content"
android:ems="10"
android:layout_weight="50" />
<EditText
android:id="@+id/editText2"
android:layout_width="0px"
android:layout_height="wrap_content"
android:ems="10"
android:layout_weight="20"
android:inputType="number" />
<EditText
android:id="@+id/editText3"
android:layout_width="0px"
android:layout_height="wrap_content"
android:ems="10"
android:layout_weight="30"
android:inputType="date" />
</TableRow>
case R.id.add:
TableLayout lout = (TableLayout) findViewById(R.id.lout);
TableRow row = new TableRow(this);
TableRow.LayoutParams objparams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 0.5f);
row.setId(300+elements);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
lout.addView(row);
EditText obj = new EditText(this);
obj.setText("text");
obj.setId(1+(29+elements));
obj.setTextColor(Color.WHITE);
EditText obj2 = new EditText(this);
obj2.setText("text");
obj2.setId(2+(29+elements));
obj2.setTextColor(Color.WHITE);
EditText obj3 = new EditText(this);
obj3.setText("text");
obj3.setId(3+(29+elements));
obj3.setTextColor(Color.WHITE);
obj.setLayoutParams(objparams);
row.setWeightSum(1.0f);
row.addView(obj);
objparams.weight = 0.2f;
obj2.setLayoutParams(objparams);
row.addView(obj2);
objparams.weight = 0.3f;
obj3.setLayoutParams(objparams);
row.addView(obj3);
elements+=3;
break;