android: 静态XML和动态加载XML混合使用,以及重写Layout控件

简介:

近期对android里面控件修改做了很多实验,由于公司需求很多,不得不重写很多控件。程序目标无非是:高效、轻巧、清晰、标准化

 

完成动态加载Layout有两种方法,依据个人喜好进行选择:

 

方法1:静态主Layout动态加载静态子Layout

 

首先构建子Layout:main2

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--布局可以任意定义,此处拿线性布局举例,里面有2个按钮元素-->  
  3. <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/menubar"   
  5.     android:background="@drawable/menubar"  
  6.     android:layout_width="wrap_content"   
  7.     android:layout_height="wrap_content">  
  8.         <!--按钮1-->  
  9.     <ImageButton android:id="@+id/button1"  
  10.         android:src="@drawable/btn1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.     ></ImageButton>  
  14.     <!--按钮2-->  
  15.     <ImageButton android:id="@+id/button2"  
  16.         android:src="@drawable/btn2"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.     ></ImageButton>  
  20. </LinearLayout>  

 

然后构建主Layout:main

 

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/background"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@drawable/background">  
  7.     <!--主Layout要给子Layout设置一个容器box,可以在此指定容器的位置,这段是关键部分-->  
  8.     <LinearLayout android:id="@+id/box"  
  9.         android:layout_alignParentBottom="true"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_centerHorizontal="true">      
  13.     </LinearLayout>  
  14. </RelativeLayout>  

 

最后在程序中加载子layout:

[java]  view plain copy
  1. public class BackgroundTest extends Activity {  
  2.     /** Called when the activity is first created. */  
  3. //      子Layout要以view的形式加入到主Layout中  
  4.     private View mBarView;  
  5. //      主Layout的容器加载子Layout的View  
  6.     private LinearLayout mLinearLayout;  
  7. //给出关键内容  
  8. public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10. //      显示主Layout  
  11.         setContentView(R.layout.main);   
  12. //      加载子Layout         
  13.         mBarView = View.inflate(this, R.layout.main2, null);  
  14. //      找到容器  
  15.         mLinearLayout = (LinearLayout)findViewById(R.id.box);  
  16. //      加上View 结束  
  17.         mLinearLayout.addView(mBarView);  
  18. }  

 

 

方法2:静态主Layout动态加载动态的Layout

首先构造你自己的子Layout和上面一样;

然后构建你自定义的Layout类:

[java]  view plain copy
  1. public class MenuLandscapeLinearLayout extends LinearLayout{   
  2. //  构造函数  
  3.     public MenuLandscapeLinearLayout(Context context) {  
  4.     super(context);  
  5.     // TODO Auto-generated constructor stub  
  6.     //加载需要的属性,加载方法一的子Layout  
  7.     ((Activity) getContext()).getLayoutInflater().inflate(R.layout.main2, this);   
  8.         //在此你可以封装很多方法   
  9.     }     
  10. }  

最后在程序中动态实例化并加载即可:

[java]  view plain copy
  1. public class BackgroundTest extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     private LinearLayout mLinearLayout;  
  4.     //声明一个子Layout View对象  
  5.     private MenuLandscapeLinearLayout mMenuLandscapeLinearLayout;  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9. //      加载主Layout  
  10.         setContentView(R.layout.main);    
  11. //      找到容器        
  12.         mLinearLayout = (LinearLayout)findViewById(R.id.box);  
  13. //      实例化一个子View  
  14.         mMenuLandscapeLinearLayout=new MenuLandscapeLinearLayout(this);  
  15. //      添加到容器  
  16.         mLinearLayout.addView(mMenuLandscapeLinearLayout);  
  17.     }  
  18. }  

至此,完成了动态加载子Layout的两种形式,里面可思考的很多,比如封装常用事件、资源,从而节省代码、节省资源;

抛砖引玉,分享经验,希望能助大家优化自己的程序。

相关文章
|
5月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
48 1
|
5月前
|
Java Android开发
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
Android Studio入门之按钮触控的解析及实战(附源码 超详细必看)(包括按钮控件、点击和长按事件、禁用与恢复按钮)
195 0
|
26天前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
|
26天前
|
XML 编解码 Java
Android控件之高级控件——ListView、cardView、屏幕适配
Android控件之高级控件——ListView、cardView、屏幕适配
|
26天前
|
Android开发
Android控件——Checkbox复选框、RadioButton单选、ToggleButton开关、SeekBar拖动条
Android控件——Checkbox复选框、RadioButton单选、ToggleButton开关、SeekBar拖动条
|
26天前
|
XML Java Android开发
Android之UI基础控件
Android之UI基础控件
|
1月前
|
XML Android开发 数据格式
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class
23 0
|
2月前
|
XML 数据格式
android.view.InflateException: Binary XML file line #0: Attempt to invoke virtual
android.view.InflateException: Binary XML file line #0: Attempt to invoke virtual
10 0
|
2月前
|
Android开发
[Android]RadioButton控件
[Android]RadioButton控件
13 0
|
4月前
|
Android开发
分享88个Android控件源代码总有一个是你想要的
分享88个Android控件源代码总有一个是你想要的
23 0