Android中自定義Topbar模板

1、Topbar模板功能介紹:自定義UI佈局,自定義UI屬性,自定義按鈕監聽事件,自定義左、右button的顯示!效果圖如下:




2、自定義屬性:values——mytopbar.xml:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <!-- 自定義屬性 -->  
  4.     <declare-styleable name="MyTopBar">  
  5.         <attr name="leftTextColor" format="color"/>  
  6.         <!-- BackGround屬性可以顏色,也可以是一個資源文件 -->  
  7.         <!-- 所以BackGround是 format="reference|color"-->  
  8.         <attr name="leftBackGround" format="reference|color"/>      
  9.         <attr name="leftText" format="string"/>  
  10.           
  11.         <attr name="title" format="string"/>  
  12.         <attr name="titleTextSize" format="dimension"/>  
  13.         <attr name="titleTextColor" format="color"/>  
  14.           
  15.         <attr name="rightTextColor" format="color"/>  
  16.         <attr name="rightBackGround" format="reference|color"/>     
  17.         <attr name="rightText" format="string"/>  
  18.     </declare-styleable>  
  19. </resources>  
3、自定義VIew:MyTopBar.java

[java] view plain copy
  1. //繼承RelativeLayout並重寫構造方法  
  2. public class MyTopBar extends RelativeLayout {  
  3.   
  4.     // 自定義的控件和自定義的屬性(values下mytopbar.xml)的聲明  
  5.     private Button leftButton, rightButton;  
  6.     private TextView tvTitle;  
  7.   
  8.     private int leftTextColor;  
  9.     private Drawable leftDrawable;  
  10.     private String leftText;  
  11.   
  12.     private float titleTextSize;  
  13.     private int titleTextColor;  
  14.     private String title;  
  15.   
  16.     private int rightTextColor;  
  17.     private Drawable rightDrawable;  
  18.     private String rightText;  
  19.   
  20.     private LayoutParams leftLayoutParams, titleLayoutParams, rightLayoutParams;  
  21.   
  22.     private myTopbarClicklistenter clicklistenter;  
  23.       
  24.     //自定義click的監聽回調接口  
  25.     public interface myTopbarClicklistenter{  
  26.         public void leftClick();  
  27.         public void rightClick();  
  28.     }  
  29.       
  30.     //自定義一個setOnClickListenter方法  
  31.     public void setOnTopbarClickListenter(myTopbarClicklistenter clicklistenter){  
  32.         this.clicklistenter=clicklistenter;   //調用的時候通過一個匿名內部類映射進來  
  33.     }  
  34.       
  35.     //重寫構造方法  
  36.     public MyTopBar(Context context, AttributeSet attrs) {  
  37.           
  38.         super(context, attrs);  
  39.           
  40.         // 獲取自定義的屬性,並將自定義的屬性映射到自定義的屬性值中去  
  41.         // 通過obtainStyledAttributes獲取自定義屬性,並存到TypedArray  
  42.         TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyTopBar);    
  43.   
  44.         leftTextColor = ta.getColor(R.styleable.MyTopBar_leftTextColor, 0);    //從TypedArray中取出來,並對應到自定義的屬性值上  
  45.         leftDrawable = ta.getDrawable(R.styleable.MyTopBar_leftBackGround);  
  46.         leftText = ta. getString(R.styleable.MyTopBar_leftText);  
  47.   
  48.         titleTextSize = ta.getDimension(R.styleable.MyTopBar_titleTextSize, 0);  
  49.         titleTextColor = ta.getColor(R.styleable.MyTopBar_titleTextColor, 0);  
  50.         title = ta.getString(R.styleable.MyTopBar_title);  
  51.   
  52.         rightTextColor = ta.getColor(R.styleable.MyTopBar_rightTextColor, 0);  
  53.         rightDrawable = ta.getDrawable(R.styleable.MyTopBar_rightBackGround);  
  54.         rightText = ta.getString(R.styleable.MyTopBar_rightText);  
  55.   
  56.         ta.recycle();  
  57.   
  58.         //組件定義  
  59.         leftButton = new Button(context);  
  60.         tvTitle = new TextView(context);  
  61.         rightButton = new Button(context);  
  62.   
  63.         // 將自定義的屬性設置到控件上  
  64.         leftButton.setTextColor(leftTextColor);  
  65.         leftButton.setBackground(leftDrawable);  
  66.         leftButton.setText(leftText);  
  67.   
  68.         tvTitle.setTextColor(titleTextColor);  
  69.         tvTitle.setTextSize(titleTextSize);  
  70.         tvTitle.setText(title);  
  71.         tvTitle.setGravity(Gravity.CENTER);    // 設置文字居中  
  72.   
  73.         rightButton.setTextColor(rightTextColor);  
  74.         rightButton.setBackground(rightDrawable);  
  75.         rightButton.setText(rightText);  
  76.   
  77.         setBackgroundColor(0xFFF59563);    // 設置背景顏色  
  78.           
  79.         //將自定義的控件放到Layout中(以LayoutParams的形式)  
  80.         leftLayoutParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
  81.         leftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);     //設置左對齊          
  82.         addView(leftButton,leftLayoutParams);  //leftButton以leftLayoutParams的形式加入到ViewGroup中  
  83.           
  84.         titleLayoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);  
  85.         titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);  //設置居中對齊        
  86.         addView(tvTitle,titleLayoutParams);    //tvTitle以titleLayoutParams的形式加入到ViewGroup中  
  87.                   
  88.         rightLayoutParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
  89.         rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE); //設置右對齊        
  90.         addView(rightButton,rightLayoutParams);//rightButton以rightLayoutParams的形式加入到ViewGroup中  
  91.           
  92.         //設置監聽事件  
  93.         leftButton.setOnClickListener(new View.OnClickListener() {  
  94.               
  95.             @Override  
  96.             public void onClick(View v) {  
  97.                 clicklistenter.leftClick();  
  98.             }  
  99.         });  
  100.           
  101.         rightButton.setOnClickListener(new View.OnClickListener() {  
  102.               
  103.             @Override  
  104.             public void onClick(View v) {  
  105.                 clicklistenter.rightClick();  
  106.             }  
  107.         });  
  108.           
  109.     }  
  110.       
  111.     //設置左Button是否顯示  
  112.     public void setLeftIsVisible(boolean flag){  
  113.         if(flag){  
  114.             leftButton.setVisibility(View.VISIBLE);  
  115.         }else{  
  116.             leftButton.setVisibility(View.GONE);  
  117.         }  
  118.     }  
  119.       
  120.     // 設置右Button是否顯示  
  121.     public void setRightIsVisible(boolean flag) {  
  122.         if (flag) {  
  123.             rightButton.setVisibility(View.VISIBLE);  
  124.         } else {  
  125.             rightButton.setVisibility(View.GONE);  
  126.         }  
  127.     }  
  128. }  
4、activity_main.xml引用自定義控件:

[html] view plain copy
  1. <!-- 引用自定義控件 -->  
  2.     <com.example.topbar.MyTopBar   
  3.         android:id="@+id/my_topbar"  
  4.         android:layout_width="match_parent"  
  5.         android:layout_height="40dp"  
  6.           
  7.         app:leftBackGround="@drawable/blue"  
  8.         app:leftTextColor="#ffffff"  
  9.         app:leftText="Back"  
  10.           
  11.         app:title="自定義標題"  
  12.         app:titleTextSize="10sp"  
  13.         app:titleTextColor="#123412"  
  14.           
  15.         app:rightBackGround="@drawable/blue"  
  16.         app:rightTextColor="#ffffff"  
  17.         app:rightText="More">  
  18.           
  19.     </com.example.topbar.MyTopBar>  
5、MainActivity.java使用控件:

[java] view plain copy
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.           
  8.         MyTopBar topBar =(MyTopBar) findViewById(R.id.my_topbar);  
  9.         //調用自定義的Topbar的自定義的click監聽事件  
  10.         topBar.setOnTopbarClickListenter(new MyTopBar.myTopbarClicklistenter() {  
  11.               
  12.             @Override  
  13.             public void leftClick() {  
  14.                 // TODO Auto-generated method stub  
  15.                 Toast.makeText(MainActivity.this"點擊了Back", Toast.LENGTH_SHORT).show();  
  16.             }  
  17.               
  18.             @Override  
  19.             public void rightClick() {  
  20.                 // TODO Auto-generated method stub  
  21.                 Toast.makeText(MainActivity.this"點擊了More", Toast.LENGTH_SHORT).show();  
  22.             }  
  23.         });  
  24.           
  25.         //topBar.setLeftIsVisible(false);  
  26.         //topBar.setRightIsVisible(false);  
  27.     }  
  28.   
  29. }  

6、使用Topbar模板提示:正如上面MainActivity中,實例化後,可以調用TopBar裏面的方法。

完整代碼下載:https://github.com/songshimvp/AndroidStudy/tree/master/TopBar
發佈了41 篇原創文章 · 獲贊 72 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章