分享

Android 在程序中动态添加 View 布局或控件

 anio17 2017-05-15

有时我们需要在程序中动态添加布局或控件等,下面用程序来展示一下相应的方法:

1、addView

添加View到布局容器

2、removeView

在布局容器中删掉已有的View

3、LayoutParams 

设置View的大小位置

 

下面来看一个demo;

  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         //setContentView(R.layout.activity_main);  
  7.           
  8.         LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);  
  9.         LinearLayout lineLayout = new LinearLayout(this);  
  10.         lineLayout.setOrientation(LinearLayout.VERTICAL);  
  11.         lineLayout.setLayoutParams(params);  
  12.         lineLayout.setGravity(Gravity.TOP );  
  13.         addView(lineLayout);  
  14.         setContentView(lineLayout);  
  15.           
  16.     }  
  17.       
  18.     private void addView(final LinearLayout lineLayout)  
  19.     {  
  20.         final TextView showText = new TextView(this);  
  21.         showText.setTextColor(Color.GREEN);  
  22.         showText.setTextSize(30);  
  23.         showText.setId(10001);//设置 id  
  24.         showText.setText("我是在程序中添加的第一个文本");  
  25.         showText.setBackgroundColor(Color.GRAY);  
  26.         // set 文本大小  
  27.         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,  
  28.                 LayoutParams.WRAP_CONTENT);  
  29.         //set 四周距离  
  30.         params.setMargins(10, 10, 10, 10);  
  31.        
  32.         showText.setLayoutParams(params);  
  33.           
  34.         //添加文本到主布局  
  35.         lineLayout.addView(showText );  
  36.           
  37.         //创建按钮  
  38.         Button btn = new Button(this);  
  39.         btn.setText("点击删除文本");  
  40.         btn.setBackgroundColor(Color.GRAY) ;  
  41.         LinearLayout.LayoutParams btn_params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,  
  42.                 LayoutParams.WRAP_CONTENT);   
  43.         btn_params.setMargins(0, 60, 60, 0);  
  44.         btn_params.gravity = Gravity.CENTER_HORIZONTAL;  
  45.         btn.setLayoutParams(btn_params);  
  46.         // 动态添加按钮到主布局  
  47.         lineLayout.addView(btn);  
  48.           
  49.         //点击按钮 动态删除文本  
  50.         btn.setOnClickListener(new OnClickListener() {  
  51.               
  52.             @Override  
  53.             public void onClick(View v) {  
  54.                 // TODO Auto-generated method stub  
  55.                 if(null != lineLayout.findViewById(10001))  
  56.                 {  
  57.                     lineLayout.removeView(showText);  
  58.                 }  
  59.                 else  
  60.                 {  
  61.                     Toast.makeText(MainActivity.this, "文本已被删除", Toast.LENGTH_SHORT).show();  
  62.                 }  
  63.             }  
  64.         });  
  65.           
  66.         //动态创建一个相对布局  
  67.         RelativeLayout relaLayout = new RelativeLayout(this);  
  68.         relaLayout.setBackgroundColor(Color.BLUE);  
  69.         RelativeLayout.LayoutParams lp11 = new RelativeLayout.LayoutParams(  
  70.                 ViewGroup.LayoutParams.MATCH_PARENT, 120);  
  71.           
  72.          
  73.         relaLayout.setLayoutParams(lp11);  
  74.         //动态创建一个文本  
  75.         final TextView RelaText = new TextView(this);  
  76.         RelaText.setTextColor(Color.GREEN);  
  77.         RelaText.setTextSize(20);  
  78.         RelaText.setText("我是在程序中添加的第二个文本,在相对布局中");  
  79.         RelaText.setBackgroundColor(Color.GRAY);  
  80.           
  81.         //设置文本的布局  
  82.         RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(  
  83.                 ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
  84.            
  85.         lp2.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);  
  86.         lp2.setMargins(10, 10, 10, 10);  
  87.         //将文本添加到相对布局中  
  88.         relaLayout.addView(RelaText,lp2);  
  89.         //将这个布局添加到主布局中  
  90.         lineLayout.addView(relaLayout);  
  91.    
  92.     }  
  93. }  

看一下效果图片:

点击按钮前:

点击按钮删除上面的文本:



demo 源代码:

http://download.csdn.NET/detail/q610098308/9293621

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多