android菜鸟学习笔记13----Android控件(二) 自定义控件简单示例
有時候,可能覺得系統提供的控件太丑,就會需要自定義控件來實現自己想要的效果。
以下主要參考《第一行代碼》
1.自定義一個標題欄:
系統自帶的標題欄很丑,且沒什么大的作用,所以我們之前會在onCreate()中調用requestWindowFeature(Window.FEATURE_NO_TITLE);設置不顯示標題欄。
下面自定義一個標題欄,中間顯示標題,左右各有一個按鈕:
title.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 5 android:layout_width="match_parent" 6 7 android:layout_height="wrap_content" 8 9 android:orientation="horizontal" 10 11 android:background="#bbbbbb" > 12 13 <Button 14 15 android:id="@+id/btn_back" 16 17 android:text="@string/back" 18 19 android:layout_width="wrap_content" 20 21 android:layout_height="wrap_content" 22 23 android:layout_margin="5dp" 24 25 android:layout_gravity="left|center_vertical" 26 27 android:textColor="#0099cc" 28 29 android:layout_weight="1"/> 30 31 <TextView 32 33 android:id="@+id/title" 34 35 android:layout_width="wrap_content" 36 37 android:layout_height="wrap_content" 38 39 android:textSize="20sp" 40 41 android:textColor="#0099cc" 42 43 android:text="@string/this_is_title" 44 45 android:layout_gravity="center" 46 47 android:gravity="center" 48 49 android:layout_weight="2"/> 50 51 <Button 52 53 android:id="@+id/btn_edit" 54 55 android:layout_width="wrap_content" 56 57 android:layout_height="wrap_content" 58 59 android:text="@string/edit" 60 61 android:layout_margin="5dp" 62 63 android:layout_gravity="right|center_vertical" 64 65 android:textColor="#0099cc" 66 67 android:layout_weight="1"/> 68 69 </LinearLayout>?Activity代碼:
1 protected void onCreate(Bundle savedInstanceState) { 2 3 super.onCreate(savedInstanceState); 4 5 requestWindowFeature(Window.FEATURE_NO_TITLE); 6 7 setContentView(R.layout.title); 8 9 }?運行結果:
?
(⊙o⊙)…有點丑哈,不過仔細看,還是有點像標題欄的。
2.復用布局代碼:
想讓這個標題欄應用在以后的每個布局文件,要怎么做呢?
總不能每次都把這些xml代碼重寫一遍吧。
android布局中提供了類似于c預處理指令#include的<include>標簽,可以實現布局代碼的復用。
下面新建一個first_layout.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 5 android:layout_width="match_parent" 6 7 android:layout_height="match_parent" 8 9 android:orientation="vertical" > 10 11 <include layout="@layout/title"/> 12 13 <Button android:id="@+id/btn" 14 15 android:text="@string/i_m_a_button" 16 17 android:layout_width="wrap_content" 18 19 android:layout_height="wrap_content" 20 21 android:layout_gravity="center_horizontal"/> 22 23 </LinearLayout>?修改setContentView(R.layout.first_layout);
顯示結果:
?
現在Back和Edit按鈕都沒有任何事件處理的,怎樣實現點擊Back按鈕就結束當前Activity呢?方法跟之前的做法完全一樣,使用findViewById()根據id找到Back按鈕,然后設置click事件監聽即可。
代碼如下:
1 public class FirstActivity extends Activity { 2 3 @Override 4 5 protected void onCreate(Bundle savedInstanceState) { 6 7 super.onCreate(savedInstanceState); 8 9 requestWindowFeature(Window.FEATURE_NO_TITLE); 10 11 setContentView(R.layout.first_layout); 12 13 Button btn = (Button) findViewById(R.id.btn_back); 14 15 btn.setOnClickListener(new OnClickListener() { 16 17 @Override 18 19 public void onClick(View v) { 20 21 // TODO Auto-generated method stub 22 23 FirstActivity.this.finish(); 24 25 } 26 27 }); 28 29 } 30 31 }?
布局文件的復用已然通過<include>實現了,但是每次都要重新寫事件監聽,還是覺得麻煩……到這里一般就會想到抽象出一個自定義類,每次需要的時候,直接使用該自定義類不就行了,其實就是自定義控件的做法了。
3.自定義控件,復用功能代碼
TitleLinearLayout.java代碼:
1 public class TitleLinearLayout extends LinearLayout { 2 3 public TitleLinearLayout(Context context, AttributeSet attrs) { 4 5 super(context, attrs); 6 7 LayoutInflater.from(context).inflate(R.layout.title, this); 8 9 Button btn_back = (Button) findViewById(R.id.btn_back); 10 11 btn_back.setOnClickListener(new OnClickListener() { 12 13 @Override 14 15 public void onClick(View v) { 16 17 // TODO Auto-generated method stub 18 19 Log.i("clicked","back"); 20 21 ((Activity)getContext()).finish(); 22 23 } 24 25 }); 26 27 } 28 29 }?繼承自LinearLayout,實現帶兩個參數的構造方法。在構造方法中,加載布局文件,并對其中的Back按鈕進行事件監聽設置。
LayoutInflater.from(context).inflate(R.layout.title, this);用于動態加載布局文件。
注意到,Activity中有一個獲取LayoutInflater的方法,所以,也可以使用下面一行代碼加載布局文件:
((Activity)context).getLayoutInflater().inflate(R.layout.title, this);這種方法,在Activity代碼中比較常用,而這里需要進行類型強轉,反倒麻煩點,而且不如第一個方法安全。
如何使用自定義的控件呢?
first_layout代碼如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 5 android:layout_width="match_parent" 6 7 android:layout_height="match_parent" 8 9 android:orientation="vertical" > 10 11 <cn.csc.custom_ui.TitleLinearLayout 12 13 android:layout_width="match_parent" 14 15 android:layout_height="wrap_content"> 16 17 </cn.csc.custom_ui.TitleLinearLayout> 18 19 <Button android:id="@+id/btn" 20 21 android:text="@string/i_m_a_button" 22 23 android:layout_width="wrap_content" 24 25 android:layout_height="wrap_content" 26 27 android:layout_gravity="center_horizontal"/> 28 29 </LinearLayout>說明:
1)在布局文件中,引用自定義的控件,需要使用完整的類限定名,即包名.類名的方式;
2)在定義控件中,設置屬性時,使用alt+/進行代碼提示補全功能將經常不可用,標簽名可以先設置為內置控件,然后進行屬性的設置,之后再把標簽名改回到自定義的控件的完整限定名即可。
?
轉載于:https://www.cnblogs.com/dqrcsc/p/4618814.html
總結
以上是生活随笔為你收集整理的android菜鸟学习笔记13----Android控件(二) 自定义控件简单示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TypeScript - Interfa
- 下一篇: Tomcat设置cmd窗口的title属