如何使用对齐的元素动态创建 LinearLayout
How create LinearLayout dynamically with aligned elements
我是 android 的新手。需要在 RadioGroup 中动态创建 LinearLayout 并对齐 TextView,如下所示。在这里我只是从调色板中移动了元素并且没有改变任何属性
但是得到这个
我错过了什么?
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
1.0f);
final LinearLayout linearLayout = new LinearLayout(this);
final TextView text1 = new TextView(this);
text1.setText("aaa");
text1.setLayoutParams(param);
linearLayout.addView(text1);
final TextView text2 = new TextView(this);
text2.setText("bbb");
text2.setLayoutParams(param);
linearLayout.addView(text2);
final TextView text3 = new TextView(this);
text3.setText("ccc");
text3.setLayoutParams(param);
linearLayout.addView(text3);
radioGroup.addView(linearLayout);
LayoutParams
应该都是 MATCH_PARENT
并且 linearLayout 也应该设置为那个 params
我是 android 的新手。需要在 RadioGroup 中动态创建 LinearLayout 并对齐 TextView,如下所示。在这里我只是从调色板中移动了元素并且没有改变任何属性
但是得到这个
我错过了什么?
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
LinearLayout.LayoutParams.WRAP_CONTENT, /* or MATCH_PARENT */
1.0f);
final LinearLayout linearLayout = new LinearLayout(this);
final TextView text1 = new TextView(this);
text1.setText("aaa");
text1.setLayoutParams(param);
linearLayout.addView(text1);
final TextView text2 = new TextView(this);
text2.setText("bbb");
text2.setLayoutParams(param);
linearLayout.addView(text2);
final TextView text3 = new TextView(this);
text3.setText("ccc");
text3.setLayoutParams(param);
linearLayout.addView(text3);
radioGroup.addView(linearLayout);
LayoutParams
应该都是 MATCH_PARENT
并且 linearLayout 也应该设置为那个 params