Android 在程序中动态添加 View 布局或控件
有时我们需要在程序中动态添加布局或控件等,下面用程序来展示一下相应的方法:
1、addView
添加View到布局容器
2、removeView
在布局容器中删掉已有的View
3、LayoutParams
设置View的大小位置
下面来看一个demo;
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); LinearLayout lineLayout = new LinearLayout(this); lineLayout.setOrientation(LinearLayout.VERTICAL); lineLayout.setLayoutParams(params); lineLayout.setGravity(Gravity.TOP ); addView(lineLayout); setContentView(lineLayout); } private void addView(final LinearLayout lineLayout) { final TextView showText = new TextView(this); showText.setTextColor(Color.GREEN); showText.setTextSize(30); showText.setId(10001);//设置 id showText.setText("我是在程序中添加的第一个文本"); showText.setBackgroundColor(Color.GRAY); // set 文本大小 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); //set 四周距离 params.setMargins(10, 10, 10, 10); showText.setLayoutParams(params); //添加文本到主布局 lineLayout.addView(showText ); //创建按钮 Button btn = new Button(this); btn.setText("点击删除文本"); btn.setBackgroundColor(Color.GRAY) ; LinearLayout.LayoutParams btn_params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); btn_params.setMargins(0, 60, 60, 0); btn_params.gravity = Gravity.CENTER_HORIZONTAL; btn.setLayoutParams(btn_params); // 动态添加按钮到主布局 lineLayout.addView(btn); //点击按钮 动态删除文本 btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(null != lineLayout.findViewById(10001)) { lineLayout.removeView(showText); } else { Toast.makeText(MainActivity.this, "文本已被删除", Toast.LENGTH_SHORT).show(); } } }); //动态创建一个相对布局 RelativeLayout relaLayout = new RelativeLayout(this); relaLayout.setBackgroundColor(Color.BLUE); RelativeLayout.LayoutParams lp11 = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 120); relaLayout.setLayoutParams(lp11); //动态创建一个文本 final TextView RelaText = new TextView(this); RelaText.setTextColor(Color.GREEN); RelaText.setTextSize(20); RelaText.setText("我是在程序中添加的第二个文本,在相对布局中"); RelaText.setBackgroundColor(Color.GRAY); //设置文本的布局 RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp2.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); lp2.setMargins(10, 10, 10, 10); //将文本添加到相对布局中 relaLayout.addView(RelaText,lp2); //将这个布局添加到主布局中 lineLayout.addView(relaLayout); } }
看一下效果图片:
点击按钮前:
点击按钮删除上面的文本:
http://download.csdn.net/detail/q610098308/9293621