Android——ViewGroup的一个用法实例(转载)

找了很久,终于找到了。 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <merge  xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge">  
  4.     <ImageView  
  5.         android:layout_width="fill_parent"  
  6.         android:layout_height="fill_parent"  
  7.         android:scaleType="center"  
  8.         android:src="@drawable/golden_gate"  
  9.   
  10.     />  
  11.     <com.example.android.merge.OkCancelBar  
  12.   
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_gravity="bottom"  
  16.         android:paddingTop="8dip"  
  17.         android:gravity="center_horizontal"  
  18.         android:background="#AA000000"  
  19.         okCancelBar:okLabel="Save"  
  20.         okCancelBar:cancelLabel="Don't save"   
  21.     />  
  22. </merge>  

 com.example.android.merge.OkCancelBar是一个自定义的GROUP

 

Java代码  收藏代码
  1. public class OkCancelBar extends LinearLayout{  
  2.   
  3.     public OkCancelBar(Context context,AttributeSet attrs){  
  4.   
  5.         super(context, attrs);  
  6.   
  7.         setOrientation(HORIZONTAL);  
  8.   
  9.         setGravity(Gravity.CENTER);  
  10.   
  11.         setWeightSum(1.0f);  
  12.   
  13.         LayoutInflater.from(context).inflate(R.layout.okcancelbar,this,true);  
  14.   
  15.   
  16.         TypedArray array= context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar,0,0);  
  17.   
  18.   
  19.         String text= array.getString(R.styleable.OkCancelBar_okLabel);  
  20.   
  21.         if(text==null) text="Ok";  
  22.   
  23.         ((Button) findViewById(R.id.okcancelbar_ok)).setText(text);  
  24.             text= array.getString(R.styleable.OkCancelBar_cancelLabel);  
  25.   
  26.         if(text==null) text="Cancel";         
  27.   
  28.         ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);  
  29.   
  30.   
  31.         array.recycle();  
  32.   
  33.   
  34.     }  
  35. }  

 

 LayoutInflater.from(context).inflate(R.layout.okcancelbar,this,true); 

直接从XML中得到一个VIEW加入到当前GROUP中 

okcancelbar.xml: 

Xml代码  收藏代码
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3. <include layout="@layout/okcancelbar_button"  android:id="@+id/okcancelbar_ok"/>  
  4. <include  layout="@layout/okcancelbar_button" android:id="@+id/okcancelbar_cancel"/>  
  5.   
  6. </merge>  
posted @ 2014-09-28 09:05  xiaochao1234  阅读(969)  评论(0编辑  收藏  举报