Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義控件與自定義屬性詳細介紹

編輯:關於 Android 程式設計

自定義控件在 Android 中無處不在,它極大地提升了開發效率。舉例來說,一個由 ImageView、ImageButton、TextView 等諸多控件組成的組合視圖,如果在很多地方都要用到,難不成每次都重寫一遍?這樣既浪費時間效率又低。這種情況下,我們就可以自定義一個 View 來替換它們,不僅提升效率,在 XML 中的運用也相當美觀。

一、控件自定義屬性介紹

以下屬性均在 values/attrs.xml 中定義,屬性名稱可隨意命名。Android 支援的屬性類型主要有以下幾種:

1. reference:參考某一資源 ID。

<declare-styleable name = "名稱">
<attr name = "background" format = "reference" />
<attr name = "src" format = "reference" />
</declare-styleable>

2. color:顏色值。

<declare-styleable name = "名稱">
<attr name = "textColor" format = "color" />
</declare-styleable>

3. boolean:布爾值。

<declare-styleable name = "名稱">
<attr name = "focusable" format = "boolean" />
</declare-styleable>

4. dimension:尺寸值。

<declare-styleable name = "名稱">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>

5. float:浮點值。

<declare-styleable name = "名稱">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>

6. integer:整型值。

<declare-styleable name = "名稱">
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
</declare-styleable>

7. string:字符串。

<declare-styleable name = "名稱">
<attr name = "text" format = "string" />
</declare-styleable>

8. fraction:百分數。

<declare-styleable name="名稱">
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
</declare-styleable>

9. enum:枚舉值。

<declare-styleable name="名稱">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>

10. flag:位或運算。

<declare-styleable name="名稱">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
</attr>
</declare-styleable>

11. 多類型(用 | 分隔)。

<declare-styleable name = "名稱">
<attr name = "background" format = "reference|color" />
</declare-styleable>

二、屬性的使用與自定義控件的實現

以下以「帶陰影的按鈕、按鈕正下方有文字說明」(類似九宮格按鈕)為例。

步驟一:構思控件組成元素,定義所需屬性。新建 values/attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="custom_view">
<attr name="custom_id" format="integer" />
<attr name="src" format="reference" />
<attr name="background" format="reference" />
<attr name="text" format="string" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>

其中 custom_view 為控件名,custom_id 為按鈕 id,src 為按鈕圖標,background 為陰影背景,text 為按鈕說明,textColor 為字體顏色,textSize 為字體大小。

步驟二:自定義控件 CustomView。透過 obtainStyledAttributes 讀取 XML 中設定的屬性值:

package com.nanlus.custom;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomView extends FrameLayout implements OnClickListener {
private CustomListener customListener = null;
private Drawable mSrc = null, mBackground = null;
private String mText = "";
private int mTextColor = 0;
private float mTextSize = 20;
private int mCustomId = 0;
private ImageView mBackgroundView = null;
private ImageButton mButtonView = null;
private TextView mTextView = null;
private LayoutParams mParams = null;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.custom_view);
mSrc = a.getDrawable(R.styleable.custom_view_src);
mBackground = a.getDrawable(R.styleable.custom_view_background);
mText = a.getString(R.styleable.custom_view_text);
mTextColor = a.getColor(R.styleable.custom_view_textColor,
Color.WHITE);
mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20);
mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0);
mTextView = new TextView(context);
mTextView.setTextSize(mTextSize);
mTextView.setTextColor(mTextColor);
mTextView.setText(mText);
mTextView.setGravity(Gravity.CENTER);
mTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mButtonView = new ImageButton(context);
mButtonView.setImageDrawable(mSrc);
mButtonView.setBackgroundDrawable(null);
mButtonView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mButtonView.setOnClickListener(this);
mBackgroundView = new ImageView(context);
mBackgroundView.setImageDrawable(mBackground);
mBackgroundView.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(mBackgroundView);
addView(mButtonView);
addView(mTextView);
this.setOnClickListener(this);
a.recycle();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mParams = (LayoutParams) mButtonView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
mButtonView.setLayoutParams(mParams);
}
mParams = (LayoutParams) mBackgroundView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
mBackgroundView.setLayoutParams(mParams);
}
mParams = (LayoutParams) mTextView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
mTextView.setLayoutParams(mParams);
}
}
public void setCustomListener(CustomListener l) {
customListener = l;
}
@Override
public void onClick(View v) {
if (customListener != null) {
customListener.onCuscomClick(v, mCustomId);
}
}
public interface CustomListener {
void onCuscomClick(View v, int custom_id);
}
}

步驟三:自定義控件的使用。main.xml 中需先宣告命名空間 xmlns:nanlus="https://schemas.android.com/apk/res/com.nanlus.custom",其中 nanlus 為 XML 中的前綴,com.nanlus.custom 為包名:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:nanlus="https://schemas.android.com/apk/res/com.nanlus.custom"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<com.nanlus.custom.CustomView
android:id="@+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
nanlus:background="@drawable/background"
nanlus:custom_id="1"
nanlus:src="@drawable/style_button"
nanlus:text="按鈕1" >
</com.nanlus.custom.CustomView>
</LinearLayout>
</RelativeLayout>

步驟四:在 Activity 中綁定點擊事件。

package com.nanlus.custom;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.nanlus.BaseActivity;
import com.nanlus.custom.CustomView.CustomListener;
public class CustomActivity extends BaseActivity implements CustomListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this);
}
@Override
public void onCuscomClick(View v, int custom_id) {
switch (custom_id) {
case 1:
Toast.makeText(this, "hello !!!", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}

透過自定義屬性,我們可以把一個複雜的控件組合封裝成一個高內聚、可重複使用的元件,並在 XML 中像使用系統控件一樣靈活配置,大幅提升了開發效率與程式碼的可維護性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved