Android 动态生成布局 (多层嵌套)

来源:互联网 发布:单片机plc哪个好学 编辑:程序博客网 时间:2024/04/30 13:07

前段时间学习了android,了解到android主要是使用xml布局文件进行布局的,但是对于一些动态的界面,就需要使用代码动态生成布局。

Android 除了可以加载xml文件,显示布局外,也可以代码生成布局,并通过setContentView(View view)方法显示布局。单独的一层布局,如一个主布局加一个控件(如Button\imageView等)动态生成代码比较简单,下面只给出示例代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.example.android_dongtaishengcheng;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9. import android.widget.RelativeLayout;  
  10. import android.widget.Toast;  
  11. import android.widget.RelativeLayout.LayoutParams;  
  12.   
  13. public class MainActivity extends Activity  
  14. {  
  15.     RelativeLayout relativeLayout = null;  
  16.     private Button button;  
  17.       
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState)  
  20.     {  
  21.     super.onCreate(savedInstanceState);  
  22.     relativeLayout = new RelativeLayout(this);  
  23.     LayoutParams params = new LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,  
  24.         RelativeLayout.LayoutParams.MATCH_PARENT);  
  25.     relativeLayout.setLayoutParams(params);  
  26.     relativeLayout.setBackgroundResource(R.color.back);  
  27.     setContentView(relativeLayout);  
  28.       
  29.     button = new Button(this);  
  30.     LayoutParams params2 = new LayoutParams(300300);  
  31.     button.setLayoutParams(params2);  
  32.     params2.addRule(RelativeLayout.CENTER_IN_PARENT);  
  33.     button.setText("hello");  
  34.     relativeLayout.addView(button);  
  35.     button.setOnClickListener(new View.OnClickListener()  
  36.     {  
  37.           
  38.         @Override  
  39.         public void onClick(View v)  
  40.         {  
  41.         // TODO Auto-generated method stub  
  42.         startActivity(new Intent(MainActivity.this,SecondActivity.class));  
  43.         }  
  44.     });  
  45.     }  
  46. }  
下面进入重点:多层嵌套布局的动态生成。

情景描述:父布局是一个线性布局,其子布局按竖直方向排列,子布局的子布局也是一个线性布局,按水平方向排列。
其实很简单,关键点在于,如何控制主布局的子布局换行显示,即实现Orientation = "vertical"。可以在子布局外在加一层布局,即下面的drawParent()方法,该方法用来生成父布局的直接子布局,drawView()方法用来生成直接子布局的子布局(该布局也是多层嵌套)。可以实现如下图效果:


贴出代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  *  
  3.  */  
  4. package com.example.android_dongtaishengcheng;  
  5. import android.R.integer;  
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.os.Bundle;  
  9. import android.view.Gravity;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.RelativeLayout;  
  14. import android.widget.LinearLayout.LayoutParams;  
  15.   
  16. /** 
  17.  * @author zhiyuan 
  18.  *  
  19.  * 2014-5-29 上午10:44:44 
  20.  *  
  21.  */  
  22. public class SecondActivity extends Activity  
  23. {  
  24.     LinearLayout layout = null;  
  25.     LinearLayout line2 = null;  
  26.     LinearLayout line3 = null;  
  27.     LinearLayout line4 = null;  
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState)  
  30.     {  
  31.     // TODO Auto-generated method stub  
  32.     super.onCreate(savedInstanceState);  
  33.     layout = new LinearLayout(this);  
  34.     LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
  35.     layout.setLayoutParams(params);  
  36.     layout.setOrientation(LinearLayout.VERTICAL);  
  37.     setContentView(layout);  
  38.   
  39.     // layout.addView(drawView(this));  
  40.     line2 = (LinearLayout) drawParent(this);  
  41.     line3 = (LinearLayout) drawParent(this);  
  42.     line4 = (LinearLayout) drawParent(this);  
  43.     for (int i = 0; i < 2; i++)  
  44.     {  
  45.         line2.addView(drawView(SecondActivity.this, i));  
  46.     }  
  47.     for (int i = 0; i < 4; i++)  
  48.     {  
  49.         line3.addView(drawView(SecondActivity.this, i));  
  50.     }  
  51.     for(int i = 0; i < 5; i++){  
  52.         line4.addView(drawView(SecondActivity.this, i));  
  53.     }  
  54.     layout.addView(line2);  
  55.     layout.addView(line3);  
  56.     layout.addView(line4);  
  57.     }  
  58.     //生成子布局的子布局  
  59.     public View drawView(Context context, int count)  
  60.     {  
  61.   
  62.     LinearLayout layout = new LinearLayout(context);  
  63.     LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,  
  64.         LayoutParams.WRAP_CONTENT, 1);  
  65.     params.gravity = Gravity.CENTER;  
  66.     layout.setOrientation(LinearLayout.HORIZONTAL);  
  67.     layout.setLayoutParams(params);  
  68.     if (count == 1)  
  69.     {  
  70.         layout.setBackgroundResource(R.color.back);  
  71.     }  
  72.     RelativeLayout relativeLayout = new RelativeLayout(context);  
  73.     android.widget.RelativeLayout.LayoutParams params3 = new android.widget.RelativeLayout.LayoutParams(  
  74.         android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,  
  75.         android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);  
  76.     relativeLayout.setLayoutParams(params3);  
  77.     /* 
  78.      * ImageView imageView = new ImageView(context); LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
  79.      */  
  80.     Button imageView = new Button(context);  
  81.     android.widget.RelativeLayout.LayoutParams params2 = new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,  
  82.         android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);  
  83.     //imageView.setGravity(RelativeLayout.CENTER_IN_PARENT);  
  84.     imageView.setText("测试");  
  85.     params2.addRule(RelativeLayout.CENTER_IN_PARENT);  
  86.       
  87.     // imageView.setBackgroundResource(R.drawable.ic_launcher);  
  88.     imageView.setLayoutParams(params2);  
  89.           
  90.     /* 
  91.      * TextView textView = new TextView(context); LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); textView.setText("测试专用"); 
  92.      * textView.setLayoutParams(params3); 
  93.      */  
  94.   
  95.     relativeLayout.addView(imageView);  
  96.     // layout.addView(textView);  
  97.         layout.addView(relativeLayout);  
  98.     return layout;  
  99.     }  
  100.     //生成主布局的子布局  
  101.     public View drawParent(Context context)  
  102.     {  
  103.     LinearLayout layout = new LinearLayout(context);  
  104.     LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);  
  105.     // params.gravity = Gravity.CENTER_HORIZONTAL;  
  106.     layout.setOrientation(LinearLayout.HORIZONTAL);  
  107.     layout.setLayoutParams(params);  
  108.     return layout;  
  109.     }  
  110.   
  111. }  

附Demo下载地址:http://download.csdn.net/detail/laoziyueguo3/7423939
0 0