定义组合控件的基本思路

1. 布局文件

 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="wrap_content"
 5     android:background="@android:color/holo_green_light"
 6     android:orientation="horizontal" >
 7     <ImageButton 
 8         android:id="@+id/ibtn_back"
 9         android:layout_width="40dp"
10         android:layout_height="40dp"
11         android:background="@drawable/selector_back_btn"
12         />
13     <TextView 
14         android:id="@+id/tv_title"
15         android:layout_width="match_parent"
16         android:layout_height="40dp"
17         android:textSize="25sp"
18         android:layout_marginLeft="20dp"
19         android:layout_gravity="center"
20         android:gravity="center_vertical"
21         android:textColor="@android:color/background_dark"
22         android:text="Test"
23         />
24 
25 </LinearLayout>

2.

 1 package com.bwf.view_06;
 2 
 3 import android.app.Activity;
 4 import android.content.Context;
 5 import android.util.AttributeSet;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.widget.ImageButton;
 9 import android.widget.LinearLayout;
10 import android.widget.TextView;
11 
12 /**
13  * 定义组合控件的基本思路
14  *         1. 创建一个布局文件用于展示组合控件的视图
15  *         2. 把需要的控件定义在该文件中
16  *         3. 将布局文件加载到我们定义的某一个容器ViewGroup
17  *         4. 在该容器中实现相关功能,并且将一些公共的操作方法提供出去
18  *         5. 在布局文件、或者java代码中去使用组合控件
19  * 
20  * */
21 public class View_Title extends LinearLayout{
22     View view;
23     ImageButton ibtn;
24     TextView tv;
25     Context context;
26     
27     public View_Title(Context context, AttributeSet attrs, int defStyleAttr) {
28         super(context, attrs, defStyleAttr);
29         // TODO Auto-generated constructor stub
30         init(context);
31     }
32 
33     public View_Title(Context context, AttributeSet attrs) {
34         super(context, attrs);
35         // TODO Auto-generated constructor stub
36         init(context);
37     }
38 
39     public View_Title(Context context) {
40         super(context);
41         // TODO Auto-generated constructor stub
42         init(context);
43     }
44 
45     private void init(final Context context) {
46         view = LayoutInflater.from(context).inflate(R.layout.view_title, null);
47         
48         ibtn = (ImageButton) view.findViewById(R.id.ibtn_back);
49         tv = (TextView) view.findViewById(R.id.tv_title);
50         
51         // 将view添加到当前LinearLayout上
52         addView(view, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
53         
54         this.context = context;
55     }
56     
57     /**
58      * 设置标题
59      * */
60     public void setTitleText(String s){
61         tv.setText(s);
62     }
63     
64     /**
65      * 获取标题内容
66      * */
67     public String getTitleText(){
68         return tv.getText()+"";
69     }
70     
71     /**
72      * 启用结束按钮
73      * */
74     public void setEnableFinishClick(){
75         ibtn.setOnClickListener(new OnClickListener() {
76             
77             @Override
78             public void onClick(View v) {
79                 ((Activity)context).finish();
80             }
81         });
82     }
83     
84 }

3.

 1 package com.bwf.view_06;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 
 7 public class MainActivity extends Activity {
 8     View_Title vt;
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13         
14         vt = (View_Title) findViewById(R.id.vt);
15         
16         // 设置组合View标题
17         vt.setTitleText("组合View展示");
18         // 启用结束activity的按钮
19         vt.setEnableFinishClick();
20     }
21 
22     @Override
23     public boolean onCreateOptionsMenu(Menu menu) {
24         // Inflate the menu; this adds items to the action bar if it is present.
25         getMenuInflater().inflate(R.menu.main, menu);
26         return true;
27     }
28 
29 }
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="@android:color/black" >
 6 
 7     <com.bwf.view_06.View_Title
 8         android:id="@+id/vt"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         />
12 
13 </RelativeLayout>

 

posted on 2015-11-09 23:04  敬的男人  阅读(142)  评论(0)    收藏  举报