New UI-Java代码动态添加控件或xml布局

New UI-Java代码动态添加控件或xml布局

 ——转载请注明出处:coder-pig,欢迎转载,请勿用于商业用途!


小猪Android开发交流群已建立,欢迎大家加入,无论是新手,菜鸟,大神都可以,小猪一个人的

力量毕竟是有限的,写出来的东西肯定会有很多纰漏不足,欢迎大家指出,集思广益,让小猪的博文

更加的详尽,帮到更多的人,O(∩_∩)O谢谢!

小猪Android开发交流群:小猪Android开发交流群群号:421858269

新Android UI实例大全目录:http://blog.csdn.net/coder_pig/article/details/42145907



本节引言:

上一节我们学过了纯Java代码来加载布局,已经有了一点动态布局的基础了,本节中

我们讲解的是,在加载了xml的基础上,来动态地添加View控件!以及动态地加载XML

布局!



本节正文:


1.Java代码动态添加控件:

动态添加组件的写法有两种,区别在于是否需要先setContentView(R.layout.activity_main);

下面演示动态地为界面添加一个Button

activity_main.xml文件的布局如下:

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/RelativeLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context="com.jay.example.trendsinflateviewdemo.MainActivity" >  
  8.   
  9.     <TextView   
  10.         android:id="@+id/txtTitle"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="我是xml文件加载的布局"/>  
  14.       
  15. </RelativeLayout>  

第一种:不需要setContentView();

[java]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.jay.example.trendsinflateviewdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.ViewGroup.LayoutParams;  
  7. import android.widget.Button;  
  8. import android.widget.RelativeLayout;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         Button btnOne = new Button(this);  
  16.         btnOne.setText("我是动态添加的按钮");  
  17.         RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(    
  18.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);    
  19.         lp2.addRule(RelativeLayout.CENTER_IN_PARENT);    
  20.         LayoutInflater inflater = LayoutInflater.from(this);  
  21.         RelativeLayout rly = (RelativeLayout) inflater.inflate(  
  22.                 R.layout.activity_main, null)  
  23.                 .findViewById(R.id.RelativeLayout1);  
  24.         rly.addView(btnOne,lp2);  
  25.         setContentView(rly);  
  26.     }  
  27. }  

第二种:需要setContentView();

[java]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.jay.example.trendsinflateviewdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.ViewGroup.LayoutParams;  
  6. import android.widget.Button;  
  7. import android.widget.RelativeLayout;  
  8.   
  9. public class MainActivity extends Activity {  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         Button btnOne = new Button(this);  
  16.         btnOne.setText("我是动态添加的按钮");  
  17.         RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(    
  18.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);    
  19.         lp2.addRule(RelativeLayout.CENTER_IN_PARENT);    
  20.         RelativeLayout rly = (RelativeLayout) findViewById(R.id.RelativeLayout1);  
  21.         rly.addView(btnOne,lp2);  
  22.     }  
  23. }  



分析总结:

代码很简单,创建按钮后,我们又创建了一个LayoutParams对象,用来设置Button的大小,又通过

addRule()方法设置了Button的位置!

第一种方法:通过LayoutInflate的inflate( )方法加载了activity_main布局,获得了外层容器,接着

addView添加按钮进容器,最后setContentView();

第二种方法:因为我们已经通过setContetView()方法加载了布局,此时我们就可以通过findViewById

找到这个外层容器,接着addView,最后setContentView()即可!

另外,关于这个setContentView( )他设置的视图节点是整个XML的根节点!



关于这个动态添加控件其实也不难,如果你看了前面那一篇<纯Java加载布局>的话,



2.Java代码动态加载xml布局


接下来的话,我们换一个,这次加载的是xml文件!动态地添加xml文件!

先写下布局文件吧:

activity_main.xml:

[java]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/RelativeLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context="com.jay.example.trendsinflateviewdemo.MainActivity" >  
  8.   
  9.     <Button   
  10.         android:id="@+id/btnLoad"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="动态加载布局"/>  
  14.       
  15. </RelativeLayout>  

动态加载的xml文件inflate.xml:

[html]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical"  
  7.     android:id="@+id/ly_inflate" >  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="我是Java代码加载的布局" />  
  13.   
  14.     <Button  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="我是布局里的一个小按钮" />  
  18.   
  19. </LinearLayout>  

接着就到我们的MainActivity.java了:

[java]   view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package com.jay.example.trendsinflateviewdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.ViewGroup.LayoutParams;  
  9. import android.widget.Button;  
  10. import android.widget.LinearLayout;  
  11. import android.widget.RelativeLayout;  
  12.   
  13. public class MainActivity extends Activity {  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         //获得LayoutInflater对象;  
  20.         final LayoutInflater inflater = LayoutInflater.from(this);    
  21.         //获得外部容器对象  
  22.         final RelativeLayout rly = (RelativeLayout) findViewById(R.id.RelativeLayout1);  
  23.         Button btnLoad = (Button) findViewById(R.id.btnLoad);  
  24.         btnLoad.setOnClickListener(new OnClickListener() {  
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 //加载要添加的布局对象  
  28.                 LinearLayout ly = (LinearLayout) inflater.inflate(  
  29.                         R.layout.inflate_ly, nullfalse).findViewById(  
  30.                         R.id.ly_inflate);  
  31.                 //设置加载布局的大小与位置  
  32.                 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(    
  33.                         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);    
  34.                 lp.addRule(RelativeLayout.CENTER_IN_PARENT);    
  35.                 rly.addView(ly,lp);  
  36.             }  
  37.         });  
  38.     }  
  39. }  

运行截图:



接下来就来分析下代码:

①获取容器对象:

final RelativeLayout rly = (RelativeLayout) findViewById(R.id.RelativeLayout1);

②获得Inflater对象,同时加载被添加的布局的xml,通过findViewById找到最外层的根节点

final LayoutInflater inflater = LayoutInflater.from(this);

LinearLayout ly = (LinearLayout) inflater.inflate(
R.layout.inflate_ly, null, false).findViewById(
R.id.ly_inflate);

③为这个容器设置大小与位置信息:

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(  
               LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
       lp.addRule(RelativeLayout.CENTER_IN_PARENT); 

④添加到外层容器中:

rly.addView(ly,lp);





好了,关于通过Java代码动态地添加控件或XML的内容大概就讲到这里!

另外,提醒一点,addView( )后,当这个View不需要的使用可以通过RemoveView( )将

这个View移除哦!安静


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值