加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:58:44 所属栏目:百科 来源:网络整理
导读:前言:以前曾写过一篇关于动态生成控件的文章 《动态添加控件及将某XML动态加入到Activity显示》 ,比较浅显,对于RelativeLayout的相关布局设置方法及相对布局与线性布局的混合使用的相关内容都没有进行深入讨论。今天再次涉及到这些内容,就不再单独讨论相

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

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

总效果:

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

一、利用XML生成一项列表

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

对应的XML代码为:

[html] view plain copy
  1. <LinearLayoutxmlns: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. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:background="#19000000"
  12. android:gravity="center_horizontal"
  13. android:paddingBottom="20dip"
  14. android:paddingTop="20dip"
  15. android:text="尝试动态生成列表"
  16. android:textColor="#ff0000"
  17. android:textSize="24sp"/>
  18. <ScrollView
  19. android:layout_width="fill_parent"
  20. android:layout_height="match_parent"
  21. android:scrollbars="vertical">
  22. <LinearLayout
  23. android:id="@+id/list_Lin"
  24. android:layout_width="fill_parent"
  25. android:layout_height="wrap_content"
  26. android:orientation="vertical">
  27. <!--动态生成部分开始-->
  28. <RelativeLayout
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content">
  31. <LinearLayout
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:layout_margin="5dip"
  35. android:layout_marginRight="10dip"
  36. android:layout_toLeftOf="@+id/image"
  37. android:background="#ff00ff00"
  38. android:orientation="horizontal"
  39. android:padding="5dip">
  40. <TextView
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="我的第一次经历"
  44. android:textColor="#ff000000"
  45. android:textSize="20dip"/>
  46. </LinearLayout>
  47. <ImageView
  48. android:id="@+id/image"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_alignParentRight="true"
  52. android:clickable="true"
  53. android:padding="5dip"
  54. android:src="@drawable/plus"/>
  55. </RelativeLayout>
  56. <!--动态生成部分结束-->
  57. </LinearLayout>
  58. </ScrollView>
  59. </LinearLayout>

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

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

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

完整代码:

[java] view plain copy
  1. packagecom.example.trydynamiclayout;
  2. /**
  3. *writebyharvic
  4. *2014-4-25
  5. *http://blog.csdn.net/harvic880925
  6. */
  7. importandroid.os.Bundle;
  8. importandroid.app.Activity;
  9. importandroid.graphics.Color;
  10. importandroid.widget.ImageView;
  11. importandroid.widget.LinearLayout;
  12. importandroid.widget.RelativeLayout;
  13. importandroid.widget.TextView;
  14. publicclassMainActivityextendsActivity{
  15. privatestaticintid=100;
  16. @Override
  17. protectedvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. finalLinearLayoutlin=(LinearLayout)findViewById(R.id.list_Lin);
  21. LinearLayout.LayoutParamsLP_FW=newLinearLayout.LayoutParams(
  22. LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
  23. RelativeLayoutnewSingleRL=newRelativeLayout(this);
  24. for(inti=0;i<10;)
  25. {
  26. newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");
  27. lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数
  28. }
  29. //finalLinearLayoutroot=(LinearLayout)findViewById(R.id.layout_root);//获取总根结点
  30. //setContentView(root);//这里必须是总根结点
  31. }
  32. /**
  33. *新建一个列表item
  34. *@paramimageID新建imageView的ID值
  35. *@paramstrTextView要显示的文字
  36. *@return新建的单项布局变量
  37. */
  38. privateRelativeLayoutgenerateSingleLayout(intimageID,Stringstr)
  39. {
  40. RelativeLayoutlayout_root_relative=newRelativeLayout(this);
  41. LinearLayoutlayout_sub_Lin=newLinearLayout(this);
  42. layout_sub_Lin.setBackgroundColor(Color.argb(0xff,0x00,0xff,0x00));
  43. layout_sub_Lin.setOrientation(LinearLayout.VERTICAL);
  44. layout_sub_Lin.setPadding(5,5,5);
  45. TextViewtv=newTextView(this);
  46. LinearLayout.LayoutParamsLP_WW=newLinearLayout.LayoutParams(
  47. LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
  48. tv.setText(str);
  49. tv.setTextColor(Color.argb(0xff,0x00));
  50. tv.setTextSize(20);
  51. tv.setLayoutParams(LP_WW);
  52. layout_sub_Lin.addView(tv);
  53. RelativeLayout.LayoutParamsRL_MW=newRelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
  54. RelativeLayout.LayoutParams.WRAP_CONTENT);//尤其注意这个位置,用的是父容器的布局参数
  55. RL_MW.setMargins(5,10,5);
  56. RL_MW.addRule(RelativeLayout.LEFT_OF,imageID);
  57. layout_root_relative.addView(layout_sub_Lin,RL_MW);
  58. ImageViewimageView=newImageView(this);
  59. RelativeLayout.LayoutParamsRL_WW=newRelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
  60. RelativeLayout.LayoutParams.WRAP_CONTENT);
  61. imageView.setPadding(5,5);
  62. RL_WW.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  63. imageView.setLayoutParams(RL_WW);
  64. imageView.setClickable(true);
  65. imageView.setId(imageID);
  66. imageView.setImageResource(R.drawable.plus);
  67. layout_root_relative.addView(imageView);
  68. returnlayout_root_relative;
  69. }
  70. }

讲解:

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

1、看这段代码:

[java] view plain copy
  1. RelativeLayoutlayout_root_relative=newRelativeLayout(this);
  2. LinearLayoutlayout_sub_Lin=newLinearLayout(this);
  3. layout_sub_Lin.setBackgroundColor(Color.argb(0xff,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
  1. finalLinearLayoutlin=(LinearLayout)findViewById(R.id.list_Lin);
  2. LinearLayout.LayoutParamsLP_FW=newLinearLayout.LayoutParams(
  3. LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
  4. RelativeLayoutnewSingleRL=newRelativeLayout(this);
  5. for(inti=0;i<10;)
  6. {
  7. newSingleRL=generateSingleLayout(id,"第"+(++i)+"个动态列表");
  8. lin.addView(newSingleRL,LP_FW);//全部用父结点的布局参数
  9. }
  10. finalLinearLayoutroot=(LinearLayout)findViewById(R.id.layout_root);//获取总根结点
  11. setContentView(root);//这里必须是总根结点

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

[java] view plain copy
  1. finalLinearLayoutlin=(LinearLayout)findViewById(R.id.list_Lin);

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

[java] view plain copy
  1. LinearLayout.LayoutParamsLP_FW=newLinearLayout.LayoutParams(
  2. LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
[java] view plain copy
  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
  1. finalLinearLayoutroot=(LinearLayout)findViewById(R.id.layout_root);//获取总根结点
  2. setContentView(root);//这里必须是总根结点

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

更正:

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

[java] view plain copy
  1. finalLinearLayoutroot=(LinearLayout)findViewById(R.id.layout_root);//获取总根结点
  2. setContentView(root);//这里必须是总根结点
其实这样做是完全没有必要的,直接将这两句删除,效果是完全一样的。

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

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

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


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

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

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

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

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

[java] view plain copy
  1. protectedvoidonCreate(BundlesavedInstanceState){
  2. super.onCreate(savedInstanceState);
  3. //setContentView(R.layout.activity_main);
  4. finalLinearLayoutlin=newLinearLayout(this);
  5. lin.setOrientation(LinearLayout.VERTICAL);
  6. LinearLayout.LayoutParamsLP_FW=newLinearLayout.LayoutParams(
  7. LinearLayout.LayoutParams.FILL_PARENT,LP_FW);//全部用父结点的布局参数
  8. }
  9. setContentView(lin);//这里必须是总根结点
  10. }
效果图:


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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读