动态添加综合布局---动态添加控件及将某XML动态加入到Activity显示(续)

来源:互联网 发布:搬家软件app 编辑:程序博客网 时间:2024/05/01 08:39


目录(?)[-]

  1. 一利用XML生成一项列表
  2. 二使用XML和JAVA代码生成界面
  3. 更正
  4. 三完全使用JAVA代码生成UI界面

前言:以前曾写过一篇关于动态生成控件的文章《动态添加控件及将某XML动态加入到Activity显示》,比较浅显,对于RelativeLayout的相关布局设置方法及相对布局与线性布局的混合使用的相关内容都没有进行深入讨论。今天再次涉及到这些内容,就不再单独讨论相对布局的相关设置内容了,直接从相对布局与线性布局的混合应用开始。

相关文章:《动态添加控件及将某XML动态加入到Activity显示》

总效果:

这里动态生成十个相同的列表,这是最终效果,但凡事都是从易而难的,下面我们就从XML生成一项内容开始讲解。

一、利用XML生成一项列表

这里先利用XML生成一项列表开始,先看一项列表的效果图及对应代码:

对应的XML代码为:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/layout_root"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity" >  
  8.   
  9.     <TextView  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="#19000000"  
  13.         android:gravity="center_horizontal"  
  14.         android:paddingBottom="20dip"  
  15.         android:paddingTop="20dip"  
  16.         android:text="尝试动态生成列表"  
  17.         android:textColor="#ff0000"  
  18.         android:textSize="24sp" />  
  19.   
  20.     <ScrollView  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="match_parent"  
  23.         android:scrollbars="vertical" >  
  24.   
  25.         <LinearLayout  
  26.             android:id="@+id/list_Lin"  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:orientation="vertical" >  
  30.   
  31.             <!-- 动态生成部分开始 -->  
  32.              <RelativeLayout  
  33.                 android:layout_width="fill_parent"  
  34.                 android:layout_height="wrap_content" >  
  35.   
  36.                 <LinearLayout  
  37.                     android:layout_width="match_parent"  
  38.                     android:layout_height="wrap_content"  
  39.                     android:layout_margin="5dip"  
  40.                     android:layout_marginRight="10dip"  
  41.                     android:layout_toLeftOf="@+id/image"  
  42.                     android:background="#ff00ff00"  
  43.                     android:orientation="horizontal"  
  44.                     android:padding="5dip" >  
  45.   
  46.                     <TextView  
  47.                         android:layout_width="wrap_content"  
  48.                         android:layout_height="wrap_content"  
  49.                         android:text="我的第一次经历"  
  50.                         android:textColor="#ff000000"  
  51.                         android:textSize="20dip" />  
  52.                 </LinearLayout>  
  53.   
  54.                 <ImageView  
  55.                     android:id="@+id/image"  
  56.                     android:layout_width="wrap_content"  
  57.                     android:layout_height="wrap_content"  
  58.                     android:layout_alignParentRight="true"  
  59.                     android:clickable="true"  
  60.                     android:padding="5dip"  
  61.                     android:src="@drawable/plus" />  
  62.             </RelativeLayout>  
  63.             <!-- 动态生成部分结束 -->  
  64.   
  65.         </LinearLayout>  
  66.     </ScrollView>  
  67.   
  68. </LinearLayout>  

动态生成注释里的部分就是我们将要用代码生成的部分,这里写出来是为了在写代码时参考,现在把注释里的部分删掉,开始在代码中生成。

二、使用XML和JAVA代码生成界面

 先贴出完整的代码,然后再逐步讲解。

 完整代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.trydynamiclayout;  
  2. /** 
  3.  * write by harvic 
  4.  * 2014-4-25 
  5.  * http://blog.csdn.net/harvic880925 
  6.  */  
  7. import android.os.Bundle;  
  8. import android.app.Activity;  
  9. import android.graphics.Color;  
  10. import android.widget.ImageView;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.RelativeLayout;  
  13. import android.widget.TextView;  
  14.   
  15. public class MainActivity extends Activity {  
  16.       
  17.     private static int id = 100;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.           
  24.         final LinearLayout lin = (LinearLayout) findViewById(R.id.list_Lin);   
  25.         LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  26.                 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  27.         RelativeLayout newSingleRL=new RelativeLayout(this);  
  28.           
  29.         for(int i=0;i<10;)  
  30.         {  
  31.             newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");  
  32.             lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数  
  33.         }  
  34.           
  35.           
  36. //      final LinearLayout root = (LinearLayout) findViewById(R.id.layout_root); //获取总根结点  
  37. //      setContentView(root); //这里必须是总根结点         
  38.     }  
  39.     /** 
  40.      * 新建一个列表item 
  41.      * @param imageID 新建imageView的ID值 
  42.      * @param str  TextView要显示的文字 
  43.      * @return 新建的单项布局变量 
  44.      */  
  45.     private RelativeLayout generateSingleLayout(int imageID,String str)  
  46.     {  
  47.         RelativeLayout layout_root_relative=new RelativeLayout(this);  
  48.           
  49.         LinearLayout layout_sub_Lin=new LinearLayout(this);  
  50.         layout_sub_Lin.setBackgroundColor(Color.argb(0xff0x000xff0x00));  
  51.         layout_sub_Lin.setOrientation(LinearLayout.VERTICAL);  
  52.         layout_sub_Lin.setPadding(5555);  
  53.           
  54.         TextView tv = new TextView(this);  
  55.         LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(  
  56.         LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  57.         tv.setText(str);  
  58.         tv.setTextColor(Color.argb(0xff0x000x000x00));  
  59.         tv.setTextSize(20);  
  60.         tv.setLayoutParams(LP_WW);  
  61.         layout_sub_Lin.addView(tv);  
  62.           
  63.         RelativeLayout.LayoutParams RL_MW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,  
  64.                 RelativeLayout.LayoutParams.WRAP_CONTENT);//尤其注意这个位置,用的是父容器的布局参数  
  65.         RL_MW.setMargins(55105);  
  66.         RL_MW.addRule(RelativeLayout.LEFT_OF,imageID);  
  67.         layout_root_relative.addView(layout_sub_Lin,RL_MW);  
  68.           
  69.           
  70.          ImageView imageView = new ImageView(this);      
  71.          RelativeLayout.LayoutParams RL_WW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  
  72.                     RelativeLayout.LayoutParams.WRAP_CONTENT);  
  73.          imageView.setPadding(5555);  
  74.          RL_WW.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  75.          imageView.setLayoutParams(RL_WW);    
  76.          imageView.setClickable(true);  
  77.          imageView.setId(imageID);  
  78.          imageView.setImageResource(R.drawable.plus);  
  79.          layout_root_relative.addView(imageView);  
  80.            
  81.          return layout_root_relative;  
  82.           
  83.     }  
  84.   
  85. }  

讲解:

 一、先看generateSingleLayout(int imageID,String str)

 1、看这段代码: 

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. RelativeLayout layout_root_relative=new RelativeLayout(this);  
  2.   
  3. LinearLayout layout_sub_Lin=new LinearLayout(this);  
  4. layout_sub_Lin.setBackgroundColor(Color.argb(0xff0x000xff0x00));  
  5. layout_sub_Lin.setOrientation(LinearLayout.VERTICAL);  
  6. layout_sub_Lin.setPadding(5555);  
  7.   
  8. TextView tv = new TextView(this);  
  9. LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(  
  10. LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  11. tv.setText(str);  
  12. tv.setTextColor(Color.argb(0xff0x000x000x00));  
  13. tv.setTextSize(20);  
  14. tv.setLayoutParams(LP_WW);  
  15. layout_sub_Lin.addView(tv);  
  16.   
  17. RelativeLayout.LayoutParams RL_MW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,  
  18.         RelativeLayout.LayoutParams.WRAP_CONTENT);//尤其注意这个位置,用的是父容器的布局参数  
  19. RL_MW.setMargins(55105);  
  20. RL_MW.addRule(RelativeLayout.LEFT_OF,imageID);  
  21. layout_root_relative.addView(layout_sub_Lin,RL_MW);  

根据上面的XML可以,我们要首先生成一个RelativeLayout,这就是layout_root_relative。
注意一: (控件的布局参数选择方式)

然后生成其第一个字布局LinearLayout  layout_sub_Lin;然后再生成layout_sub_Lin里唯一的一个控件,注意这里设置LayoutParams的方式,使用的是LinearLayout 参数!!!!对于如何选择当前控件的布局layout_width、layout_height的参数的方法,主要是看其父布局!!!!如果其父布局是LinearLayout 设置其LayoutParams参数时就要使用LinearLayout.LayoutParams,正如这里的TextView tv。而如果其父容器的相对布局呢,一样,换它父布局的来,使用RelativeLayout.LayoutParams RL_MW,如这里的LinearLayout layout_sub_Lin,所以即便layout_sub_Lin自己是布局控件也要按其父容器的布局方法写!!!!  

注意二: layout_toLeftOf的代码书写方法 

在XML中,对于此LinearLayout的相对布局,用到了android:layout_toLeftOf="@+id/image",而在代码中是动态生成的控件,如何利用此规则呢。
首先给动态生成的ImageView设置一个ID值,此ID值在些Acitivity中必须是唯一的,不可冲突的,如果冲突,关于用到此ID值的任何代码都将是无效的!这也就是后面代码中会看到的imageView.setId(imageID);
然后利用addRule()添加规则。

 2、剩余代码就没什么好讲的了,就是生成一个imageView设置ID值及其它参数,然后添加到RelativeLayout中,并将layout_root_relative返回。

 二、onCreate()函数

 这段代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. final LinearLayout lin = (LinearLayout) findViewById(R.id.list_Lin);   
  2. LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  3.         LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  4. RelativeLayout newSingleRL=new RelativeLayout(this);  
  5.   
  6. for(int i=0;i<10;)  
  7. {  
  8.     newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");  
  9.     lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数  
  10. }  
  11.   
  12.   
  13. final LinearLayout root = (LinearLayout) findViewById(R.id.layout_root); //获取总根结点  
  14. setContentView(root); //这里必须是总根结点     

1、先看For循环及其上部的代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. final LinearLayout lin = (LinearLayout) findViewById(R.id.list_Lin);   

找到当前新生成的ITEM项的插入位置。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  2.         LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);   
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");  
  2. lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数  

这里是两块代码,先看第二块,先是新生成一项,注意这一项返回的结点是RelativeLayout layout_root_relative,然后将其插入到列表位置中去,注意要插入的布局是LinearLayout lin,也就是layout_root_relative的父结点是LinearLayout,所以这也就是在addView时为什么它对应的布局参数使用LinearLayout.LayoutParams的原因了!
 2、setContentView(root);显示视图 

 这段代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. final LinearLayout root = (LinearLayout) findViewById(R.id.layout_root); //获取总根结点  
  2. setContentView(root); //这里必须是总根结点         

这里最注意的一点,setContentView()所设置的视图结点是整个XML的根结点!!!!设置为其它结点会发生异常!!!很容易理解。

 

更正:

在原来的onCreate代码中,在代码的最后,加上了

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. final LinearLayout root = (LinearLayout) findViewById(R.id.layout_root); //获取总根结点  
  2. setContentView(root); //这里必须是总根结点     
其实这样做是完全没有必要的,直接将这两句删除,效果是完全一样的。

原因在于,在其上面的代码中,我们通过

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. final LinearLayout lin = (LinearLayout) findViewById(R.id.list_Lin);  
找到了要插入布局的结点位置,直接在其下面插入布局代码,界面会自动更新,根本不需要重新setContentView()

在博客中,我将这两句无关代码注释了起来,而源码中没有更改过来,请大家注意一下,由于当时刚接触这部分,对大家造成的误导,深表歉意……


 (源码中有两句代码完全不必要加,请看博客“更正”部分)

 源码下载地址:http://download.csdn.net/detail/harvic880925/7250631,不要分,仅供分享!

 

三、完全使用JAVA代码生成UI界面

这部分其实在上面的改动不大,只是完全使用代码构建整个界面,由于这种方法构建UI可维护性很差,所以不推荐使用。

其它代码不变,OnCreate()函数代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.     protected void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3. //      setContentView(R.layout.activity_main);  
  4.           
  5.         final LinearLayout lin = new LinearLayout(this);  
  6.         lin.setOrientation(LinearLayout.VERTICAL);  
  7.         LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  8.                 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  9.         RelativeLayout newSingleRL=new RelativeLayout(this);  
  10.           
  11.         for(int i=0;i<10;)  
  12.         {  
  13.             newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");  
  14.             lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数  
  15.         }  
  16.           
  17.         setContentView(lin); //这里必须是总根结点          
  18.     }  
效果图:


该部分源码地址:http://download.csdn.net/detail/harvic880925/7692059


目录(?)[-]

  1. 一利用XML生成一项列表
  2. 二使用XML和JAVA代码生成界面
  3. 更正
  4. 三完全使用JAVA代码生成UI界面

前言:以前曾写过一篇关于动态生成控件的文章《动态添加控件及将某XML动态加入到Activity显示》,比较浅显,对于RelativeLayout的相关布局设置方法及相对布局与线性布局的混合使用的相关内容都没有进行深入讨论。今天再次涉及到这些内容,就不再单独讨论相对布局的相关设置内容了,直接从相对布局与线性布局的混合应用开始。

相关文章:《动态添加控件及将某XML动态加入到Activity显示》

总效果:

这里动态生成十个相同的列表,这是最终效果,但凡事都是从易而难的,下面我们就从XML生成一项内容开始讲解。

一、利用XML生成一项列表

这里先利用XML生成一项列表开始,先看一项列表的效果图及对应代码:

对应的XML代码为:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/layout_root"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity" >  
  8.   
  9.     <TextView  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="#19000000"  
  13.         android:gravity="center_horizontal"  
  14.         android:paddingBottom="20dip"  
  15.         android:paddingTop="20dip"  
  16.         android:text="尝试动态生成列表"  
  17.         android:textColor="#ff0000"  
  18.         android:textSize="24sp" />  
  19.   
  20.     <ScrollView  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="match_parent"  
  23.         android:scrollbars="vertical" >  
  24.   
  25.         <LinearLayout  
  26.             android:id="@+id/list_Lin"  
  27.             android:layout_width="fill_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:orientation="vertical" >  
  30.   
  31.             <!-- 动态生成部分开始 -->  
  32.              <RelativeLayout  
  33.                 android:layout_width="fill_parent"  
  34.                 android:layout_height="wrap_content" >  
  35.   
  36.                 <LinearLayout  
  37.                     android:layout_width="match_parent"  
  38.                     android:layout_height="wrap_content"  
  39.                     android:layout_margin="5dip"  
  40.                     android:layout_marginRight="10dip"  
  41.                     android:layout_toLeftOf="@+id/image"  
  42.                     android:background="#ff00ff00"  
  43.                     android:orientation="horizontal"  
  44.                     android:padding="5dip" >  
  45.   
  46.                     <TextView  
  47.                         android:layout_width="wrap_content"  
  48.                         android:layout_height="wrap_content"  
  49.                         android:text="我的第一次经历"  
  50.                         android:textColor="#ff000000"  
  51.                         android:textSize="20dip" />  
  52.                 </LinearLayout>  
  53.   
  54.                 <ImageView  
  55.                     android:id="@+id/image"  
  56.                     android:layout_width="wrap_content"  
  57.                     android:layout_height="wrap_content"  
  58.                     android:layout_alignParentRight="true"  
  59.                     android:clickable="true"  
  60.                     android:padding="5dip"  
  61.                     android:src="@drawable/plus" />  
  62.             </RelativeLayout>  
  63.             <!-- 动态生成部分结束 -->  
  64.   
  65.         </LinearLayout>  
  66.     </ScrollView>  
  67.   
  68. </LinearLayout>  

动态生成注释里的部分就是我们将要用代码生成的部分,这里写出来是为了在写代码时参考,现在把注释里的部分删掉,开始在代码中生成。

二、使用XML和JAVA代码生成界面

 先贴出完整的代码,然后再逐步讲解。

 完整代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.example.trydynamiclayout;  
  2. /** 
  3.  * write by harvic 
  4.  * 2014-4-25 
  5.  * http://blog.csdn.net/harvic880925 
  6.  */  
  7. import android.os.Bundle;  
  8. import android.app.Activity;  
  9. import android.graphics.Color;  
  10. import android.widget.ImageView;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.RelativeLayout;  
  13. import android.widget.TextView;  
  14.   
  15. public class MainActivity extends Activity {  
  16.       
  17.     private static int id = 100;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.           
  24.         final LinearLayout lin = (LinearLayout) findViewById(R.id.list_Lin);   
  25.         LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  26.                 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  27.         RelativeLayout newSingleRL=new RelativeLayout(this);  
  28.           
  29.         for(int i=0;i<10;)  
  30.         {  
  31.             newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");  
  32.             lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数  
  33.         }  
  34.           
  35.           
  36. //      final LinearLayout root = (LinearLayout) findViewById(R.id.layout_root); //获取总根结点  
  37. //      setContentView(root); //这里必须是总根结点         
  38.     }  
  39.     /** 
  40.      * 新建一个列表item 
  41.      * @param imageID 新建imageView的ID值 
  42.      * @param str  TextView要显示的文字 
  43.      * @return 新建的单项布局变量 
  44.      */  
  45.     private RelativeLayout generateSingleLayout(int imageID,String str)  
  46.     {  
  47.         RelativeLayout layout_root_relative=new RelativeLayout(this);  
  48.           
  49.         LinearLayout layout_sub_Lin=new LinearLayout(this);  
  50.         layout_sub_Lin.setBackgroundColor(Color.argb(0xff0x000xff0x00));  
  51.         layout_sub_Lin.setOrientation(LinearLayout.VERTICAL);  
  52.         layout_sub_Lin.setPadding(5555);  
  53.           
  54.         TextView tv = new TextView(this);  
  55.         LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(  
  56.         LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  57.         tv.setText(str);  
  58.         tv.setTextColor(Color.argb(0xff0x000x000x00));  
  59.         tv.setTextSize(20);  
  60.         tv.setLayoutParams(LP_WW);  
  61.         layout_sub_Lin.addView(tv);  
  62.           
  63.         RelativeLayout.LayoutParams RL_MW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,  
  64.                 RelativeLayout.LayoutParams.WRAP_CONTENT);//尤其注意这个位置,用的是父容器的布局参数  
  65.         RL_MW.setMargins(55105);  
  66.         RL_MW.addRule(RelativeLayout.LEFT_OF,imageID);  
  67.         layout_root_relative.addView(layout_sub_Lin,RL_MW);  
  68.           
  69.           
  70.          ImageView imageView = new ImageView(this);      
  71.          RelativeLayout.LayoutParams RL_WW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  
  72.                     RelativeLayout.LayoutParams.WRAP_CONTENT);  
  73.          imageView.setPadding(5555);  
  74.          RL_WW.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  75.          imageView.setLayoutParams(RL_WW);    
  76.          imageView.setClickable(true);  
  77.          imageView.setId(imageID);  
  78.          imageView.setImageResource(R.drawable.plus);  
  79.          layout_root_relative.addView(imageView);  
  80.            
  81.          return layout_root_relative;  
  82.           
  83.     }  
  84.   
  85. }  

讲解:

 一、先看generateSingleLayout(int imageID,String str)

 1、看这段代码: 

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. RelativeLayout layout_root_relative=new RelativeLayout(this);  
  2.   
  3. LinearLayout layout_sub_Lin=new LinearLayout(this);  
  4. layout_sub_Lin.setBackgroundColor(Color.argb(0xff0x000xff0x00));  
  5. layout_sub_Lin.setOrientation(LinearLayout.VERTICAL);  
  6. layout_sub_Lin.setPadding(5555);  
  7.   
  8. TextView tv = new TextView(this);  
  9. LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(  
  10. LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  11. tv.setText(str);  
  12. tv.setTextColor(Color.argb(0xff0x000x000x00));  
  13. tv.setTextSize(20);  
  14. tv.setLayoutParams(LP_WW);  
  15. layout_sub_Lin.addView(tv);  
  16.   
  17. RelativeLayout.LayoutParams RL_MW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,  
  18.         RelativeLayout.LayoutParams.WRAP_CONTENT);//尤其注意这个位置,用的是父容器的布局参数  
  19. RL_MW.setMargins(55105);  
  20. RL_MW.addRule(RelativeLayout.LEFT_OF,imageID);  
  21. layout_root_relative.addView(layout_sub_Lin,RL_MW);  

根据上面的XML可以,我们要首先生成一个RelativeLayout,这就是layout_root_relative。
注意一: (控件的布局参数选择方式)

然后生成其第一个字布局LinearLayout  layout_sub_Lin;然后再生成layout_sub_Lin里唯一的一个控件,注意这里设置LayoutParams的方式,使用的是LinearLayout 参数!!!!对于如何选择当前控件的布局layout_width、layout_height的参数的方法,主要是看其父布局!!!!如果其父布局是LinearLayout 设置其LayoutParams参数时就要使用LinearLayout.LayoutParams,正如这里的TextView tv。而如果其父容器的相对布局呢,一样,换它父布局的来,使用RelativeLayout.LayoutParams RL_MW,如这里的LinearLayout layout_sub_Lin,所以即便layout_sub_Lin自己是布局控件也要按其父容器的布局方法写!!!!  

注意二: layout_toLeftOf的代码书写方法 

在XML中,对于此LinearLayout的相对布局,用到了android:layout_toLeftOf="@+id/image",而在代码中是动态生成的控件,如何利用此规则呢。
首先给动态生成的ImageView设置一个ID值,此ID值在些Acitivity中必须是唯一的,不可冲突的,如果冲突,关于用到此ID值的任何代码都将是无效的!这也就是后面代码中会看到的imageView.setId(imageID);
然后利用addRule()添加规则。

 2、剩余代码就没什么好讲的了,就是生成一个imageView设置ID值及其它参数,然后添加到RelativeLayout中,并将layout_root_relative返回。

 二、onCreate()函数

 这段代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. final LinearLayout lin = (LinearLayout) findViewById(R.id.list_Lin);   
  2. LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  3.         LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  4. RelativeLayout newSingleRL=new RelativeLayout(this);  
  5.   
  6. for(int i=0;i<10;)  
  7. {  
  8.     newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");  
  9.     lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数  
  10. }  
  11.   
  12.   
  13. final LinearLayout root = (LinearLayout) findViewById(R.id.layout_root); //获取总根结点  
  14. setContentView(root); //这里必须是总根结点     

1、先看For循环及其上部的代码:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. final LinearLayout lin = (LinearLayout) findViewById(R.id.list_Lin);   

找到当前新生成的ITEM项的插入位置。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  2.         LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);   
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");  
  2. lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数  

这里是两块代码,先看第二块,先是新生成一项,注意这一项返回的结点是RelativeLayout layout_root_relative,然后将其插入到列表位置中去,注意要插入的布局是LinearLayout lin,也就是layout_root_relative的父结点是LinearLayout,所以这也就是在addView时为什么它对应的布局参数使用LinearLayout.LayoutParams的原因了!
 2、setContentView(root);显示视图 

 这段代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. final LinearLayout root = (LinearLayout) findViewById(R.id.layout_root); //获取总根结点  
  2. setContentView(root); //这里必须是总根结点         

这里最注意的一点,setContentView()所设置的视图结点是整个XML的根结点!!!!设置为其它结点会发生异常!!!很容易理解。

 

更正:

在原来的onCreate代码中,在代码的最后,加上了

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. final LinearLayout root = (LinearLayout) findViewById(R.id.layout_root); //获取总根结点  
  2. setContentView(root); //这里必须是总根结点     
其实这样做是完全没有必要的,直接将这两句删除,效果是完全一样的。

原因在于,在其上面的代码中,我们通过

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. final LinearLayout lin = (LinearLayout) findViewById(R.id.list_Lin);  
找到了要插入布局的结点位置,直接在其下面插入布局代码,界面会自动更新,根本不需要重新setContentView()

在博客中,我将这两句无关代码注释了起来,而源码中没有更改过来,请大家注意一下,由于当时刚接触这部分,对大家造成的误导,深表歉意……


 (源码中有两句代码完全不必要加,请看博客“更正”部分)

 源码下载地址:http://download.csdn.net/detail/harvic880925/7250631,不要分,仅供分享!

 

三、完全使用JAVA代码生成UI界面

这部分其实在上面的改动不大,只是完全使用代码构建整个界面,由于这种方法构建UI可维护性很差,所以不推荐使用。

其它代码不变,OnCreate()函数代码如下:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.     protected void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3. //      setContentView(R.layout.activity_main);  
  4.           
  5.         final LinearLayout lin = new LinearLayout(this);  
  6.         lin.setOrientation(LinearLayout.VERTICAL);  
  7.         LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(  
  8.                 LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  9.         RelativeLayout newSingleRL=new RelativeLayout(this);  
  10.           
  11.         for(int i=0;i<10;)  
  12.         {  
  13.             newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");  
  14.             lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数  
  15.         }  
  16.           
  17.         setContentView(lin); //这里必须是总根结点          
  18.     }  
效果图:


该部分源码地址:http://download.csdn.net/detail/harvic880925/7692059


0 0
原创粉丝点击