android學習七(建立自訂控制項),android控制項
前面學習的是android的基本控制項和布局的使用,但是基本的控制項和布局有時候並不能實現複雜的布局。我們來看下各種控制項和布局的關係。
可見所有的控制項都是直接或者間接的繼承自View的,所有的布局都是直接或者間接基礎自ViewGroup的。View是Android中一種最基本的UI組件,它可以在螢幕的上繪製一塊矩形地區,並能響應這塊地區的各種事件,因此,我們使用的各種控制項其實就是在View的基礎上又添加了各種各自特有的功能。而ViewGroup則是一種特殊的View,它可以包含很多的View和子ViewGroup,是一個用於放置控制項和布局的容器。建立自訂控制項的兩種簡單的方法,一種是引入布局,另一種是自訂控制項。
首先建立一個項目,項目的名字為UICustomViews。
下面我們就自己定義一個標題列。
建立一個布局,代碼如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@drawable/title_bg" > <Button android:id="@+id/title_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:background="@drawable/back_bg" android:text="Back" android:textColor="#fff" /> <TextView android:id="@+id/title_text" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="Title Text" android:textColor="#fff" android:textSize="24sp" /> <Button android:id="@+id/title_edit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="5dip" android:background="@drawable/edit_bg" android:text="Edit" android:textColor="#fff" /> </LinearLayout>
修改主布局,代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <include layout="@layout/title"/></LinearLayout>
修改MainActivity裡面的onCreate方法
代碼如下:
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);}
<include layout="@layout/title"/>是引入一個布局檔案
requestWindowFeature(Window.FEATURE_NO_TITLE);是將系統內建的標題列隱藏掉
程式運行結果如下:
使用上面的這種引入布局的方式,不管有多少布局需要添加標題列,只需一行include語句就可以了。
引入布局的技巧確實解決了重複編寫代碼的問題,但是如果布局中有一些控制項要求能響應事件,我們還是需要在每個活動中為這些控制項單獨的編寫一次事件註冊的代碼。但是註冊的代碼都一樣,所以這會帶來很多重複的代碼,那麼下面就進行自訂控制項。
自訂控制項
建立TitleLayout繼承LinearLayout,代碼和說明如下:
package com.wj.uicustomviews;import android.app.Activity;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.Toast;public class TitleLayout extends LinearLayout {public TitleLayout(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub/* * 重寫LinearLayout中的帶有2個參數的建構函式,在布局中引入TitleLayout控制項就會調用這個建構函式 * 然後在建構函式中需要對標題列布局進行動態載入,這就是要藉助LayoutInflater來實現了。通過LayoutInflater的 * from()方法可以構建出一個LayoutInflater對象,然後調用inflate()方法就可以動態載入一個布局檔案,inflate方法 * 接收2個參數數,第一個參數是要載入的布局的id,這裡我們傳入R.layout.title,第二個參數是給載入好的布局在添加一個父布局, * 這裡我們想要指定為TitleLayout,於是就傳入this * * */LayoutInflater.from(context).inflate(R.layout.title, this);//對按鈕進行事件處理Button titleBack=(Button) findViewById(R.id.title_back);Button titleEdit=(Button) findViewById(R.id.title_edit);titleBack.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stub((Activity)getContext()).finish();}});titleEdit.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubToast.makeText(getContext(), "you clicked edit button",Toast.LENGTH_SHORT).show();}});}}
在布局檔案中引用自訂控制項修改主布局檔案,代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- <include layout="@layout/title"/> --> <com.wj.uicustomviews.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" > </com.wj.uicustomviews.TitleLayout></LinearLayout>
運行結果如下:
單擊按鈕會有相應的事件響應。
注意,添加自訂控制項的時候需要指明完整的包名和類名
轉載請註明:http://blog.csdn.net/j903829182/article/details/40682583
對於安卓自訂控制項的問題?
繼承View,主要是定義一個控制項,比如嫌安卓內建的button不合適,可以繼承view自己實現個。
繼承ViewGroup或LinaryLayout,就是自訂一個布局,繼承ViewGroup自己要實現更多功能
對於android自訂控制項開發的問題???
可以給RelativeLayout設定id,然後在java檔案中設定onclickListener啊,TextView也可以這樣。