《Android第一行代碼》學習記錄008 - 建立自訂控制項

來源:互聯網
上載者:User

標籤:

一、關於View與布局,首先

可以看到:

  1. View是Android中最基本的UI組件,它可以在螢幕上繪製一塊矩形地區,並能響應這個地區的各種事件;
  2. ViewGroup是一種特殊的View,它可以包含很多子View和子ViewGroup,是一種用於放置控制項和布局的容器;
  3. 我們所使用的所有控制項都是直接或間接繼承View的,各種控制項其實就是在View的基礎上添加了各自的功能;
  4. 所有布局都是直接繼承自ViewGroup的;

二、定義自訂控制項需要:

  • 自訂控制項的布局檔案;
  • 自訂控制項的類;

以自訂一個標題列為例,效果如(請無視它的難看程度)

自訂布局檔案:

<?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:background="#444"    android:orientation="horizontal" >    <Button        android:id="@+id/btnBack"        android:layout_width="33dip"        android:layout_height="33dip"        android:background="@drawable/icon_back" />    <TextView        android:id="@+id/tvTitle"        android:layout_width="0dip"        android:layout_height="33dip"        android:layout_weight="1"        android:background="#444"        android:gravity="center"        android:text="標題列"        android:textColor="#fff" />    <Button        android:id="@+id/btnEdit"        android:layout_width="33dip"        android:layout_height="33dip"        android:background="@drawable/icon_edit" /></LinearLayout>

自訂控制項的類:

package com.matclone.CustomUIViewTest;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.TextView;import android.widget.Toast;public class TitleBar extends LinearLayout {    private Button btnBack = null;    private TextView tvTitle = null;    private Button btnEdit = null;    public TitleBar(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub        LayoutInflater.from(context).inflate(R.layout.title_bar, this);                btnBack = (Button) this.findViewById(R.id.btnBack);        tvTitle = (TextView) this.findViewById(R.id.tvTitle);        btnEdit = (Button) this.findViewById(R.id.btnEdit);                btnBack.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                ((Activity)getContext()).finish();            }        });                btnEdit.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                Toast.makeText(getContext(), "你點擊了EDIT按鈕", Toast.LENGTH_SHORT).show();            }        });    }        public void setTitle(String title) {        this.tvTitle.setText(title);    }}

三、使用自訂控制項需要:

  • 在布局檔案中引入自訂控制項;

在其他的活動的布局檔案中引入自訂控制項:

<?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="match_parent"    android:orientation="vertical" >    <com.matclone.CustomUIViewTest.TitleBar        android:id="@+id/titleBar"        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </com.matclone.CustomUIViewTest.TitleBar></LinearLayout>

 

《Android第一行代碼》學習記錄008 - 建立自訂控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.