AndroidUI之使⽤java代码实现动态添加LinearLayout(⼀)先看⼀下要实现的最终效果图:
so,,,现在要使⽤java代码去动态添加这些控件,并实现如上效果
很简单的。。。看代码。。。
/**
* 动态添加线性布局
*/
private void addLinearLayout() {
/
/initMissionList:存储⼏条测试数据
for (int i = 0; i < initMissionList().size(); i++) {
//LinearLayout默认是⽔平(0)居中,现在改为垂直居中
llFmhpMissionList.setOrientation(1);
//实例化⼀个LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
//设置LinearLayout属性(宽和⾼)
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 120);
//设置边距
layoutParams.setMargins(54, 0, 84, 0);
//将以上的属性赋给LinearLayout
linearLayout.setLayoutParams(layoutParams);
//实例化⼀个TextView
TextView tv = new TextView(this);
//设置宽⾼以及权重
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutPar            //设置textview垂直居中
tv.setLayoutParams(tvParams);
tv.setTextSize(14);
tv.setTextColor(getResources().lor.rbtn_tet));
tv.setText(initMissionList().get(i).toString().trim());
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setLayoutParams(new RadioGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 120));
radioGroup.setOrientation(0);
RadioGroup.LayoutParams rbtnParams = new RadioGroup.LayoutParams(72, 72);
RadioGroup.LayoutParams rbtnParamsf = new RadioGroup.LayoutParams(72, 72);
rbtnParamsf.leftMargin=84;
rbtnParamsf.rightMargin=90;
final RadioButton radioButtonF = new RadioButton(this);
radioButtonF.setLayoutParams(rbtnParamsf);
radioButtonF.setButtonDrawable(ansparent);
radioButtonF.setBackground(getResources().getDrawable(R.drawable.selector_fmhp_radiobutton_x_style));
final RadioButton radioButtonT = new RadioButton(this);
radioButtonT.setLayoutParams(rbtnParams);
radioButtonT.setButtonDrawable(ansparent);
radioButtonT.setBackground(getResources().getDrawable(R.drawable.selector_fmhp_radiobutton_hook_style));
radioGroup.addView(radioButtonF);
radioGroup.addView(radioButtonT);
linearLayout.addView(tv);
linearLayout.addView(radioGroup);
llFmhpMissionList.addView(linearLayout);
}
}
android radiogroup好了,基本介绍到这。。。相对来说还是⽐较简单。。。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。