安卓自訂控制項的實現

來源:互聯網
上載者:User

標籤:

首先自訂控制項有什麼用呢?當有一種群組控制項在很多活動的布局中都要使用到的時候,如果沒有自訂控制項,那麼在每個活動的布局都要寫一次重複的代碼,這樣子就會使代碼累贅。

這時就要使用到自訂控制項了,自訂控制項的步驟如下:

第一步

完成自訂控制項的布局檔案(.xml檔案)

如下面的title.xml

<?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" >    <Button        android:id="@+id/btn_back"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="5dp"        android:text="退出" />    <TextView        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:gravity="center"        android:textSize="20sp"        android:layout_weight="1"        android:text="標題內容" />    <Button        android:id="@+id/btn_next"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="5dp"        android:text="next" />    </LinearLayout>

第二步

定義一個類繼承於ViewGroup下的任意子類,一般為LinearLayout(必須重寫LinearLayout中的帶兩個參數的構造方法,在布局中引入這個TitleLayout控制項就會調用這個建構函式。LayoutInflater的inflater方法中接收兩個參數,一個是要載入的布局檔案的id,一個是給載入好的布局添加一個父布局,這裡是我們要指定為TitleLayout,於是直接傳入this)

package com.jsako.ui;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;import com.jsako.R;public class TitleLayout extends LinearLayout {public TitleLayout(Context context, AttributeSet attrs) {super(context, attrs);View view=LayoutInflater.from(context).inflate(R.layout.title,this);init(view);}private void init(View view) {Button btn_back=(Button) findViewById(R.id.btn_back);Button btn_next=(Button) findViewById(R.id.btn_next);btn_back.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {((Activity)getContext()).finish();}});btn_next.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getContext(),"你點擊了next", 0).show();}});}}

 第三步

使用這個自訂控制項(添加自訂控制項和添加普通控制項的方式基本一樣,只不過在添加自訂控制項的時候我們需要指明控制項的完整類名,包名在這裡是不可以省略的)

<RelativeLayout 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"    tools:context="${relativePackage}.${activityClass}" >    <com.jsako.ui.TitleLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </com.jsako.ui.TitleLayout></RelativeLayout>

 

安卓自訂控制項的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.