閱讀310 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android: 靜態XML和動態加載XML混合使用,以及重寫Layout控件

近期對android裏麵控件修改做了很多實驗,由於公司需求很多,不得不重寫很多控件。程序目標無非是:高效、輕巧、清晰、標準化

 

完成動態加載Layout有兩種方法,依據個人喜好進行選擇:

 

方法1:靜態主Layout動態加載靜態子Layout

 

首先構建子Layout:main2

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--布局可以任意定義,此處拿線性布局舉例,裏麵有2個按鈕元素-->  
  3. <LinearLayout  xmlns:android="https://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 plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="https://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 plaincopy
  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 plaincopy
  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 plaincopy
  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的兩種形式,裏麵可思考的很多,比如封裝常用事件、資源,從而節省代碼、節省資源;

拋磚引玉,分享經驗,希望能助大家優化自己的程序。

最後更新:2017-04-02 16:48:06

  上一篇:go android中MotionEvent.ACTION_CANCEL事件如何被觸發?
  下一篇:go android:MotionEvent