android自定义组件属性,android自定义控件并添加属性的方法以及示例
安卓系統(tǒng)為我們提供了豐富的控件,但是在實(shí)際項(xiàng)目中我們?nèi)匀恍枰匦峦ㄟ^(guò)布局來(lái)實(shí)現(xiàn)一些效果,比如我們需要一個(gè)上面圖標(biāo),下面文字的button,類似于下面這樣的:
最直接的解決辦法是通過(guò)將imageview和textview放在一個(gè)垂直排列的LinearLayout中,如下:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
android:id="@+id/icon_part"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
android:id="@+id/text_part"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#000000" />
但是每一個(gè)button都需要這么長(zhǎng)的代碼,上面三個(gè)按鈕的話就需要重復(fù)寫三次,而且別人一看是個(gè)LinearLayout,不會(huì)將它button聯(lián)系起來(lái)。
如果有一種辦法能將上面那個(gè)布局組合成一個(gè)控件就好了。
的確是有辦法的。主要有兩方面的工作。
1.新建一個(gè)繼承自LinearLayout的類(也可以是其他布局類,不過(guò)LinearLayout好像比較合適),然后通過(guò)inflater在這個(gè)類的構(gòu)造函數(shù)中將上面的布局添加進(jìn)去。
2.為了能在xml中也給這個(gè)自定義控件賦予屬性來(lái)獲得現(xiàn)實(shí)效果,比如字體大小、圖標(biāo)資源等,我們還需要在attrs文件中申明一些自定義屬性。你可以查閱declare-styleable了解這是怎么回事。
我這里有一個(gè)已經(jīng)實(shí)現(xiàn)了這種button效果的類FlexImageButton:package com.jcodecraeer.client.widget;
import com.jcodecraeer.client.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FlexImageButton extends LinearLayout {
private ImageView imageView;
private TextView textView;
private CharSequence text;
private Drawable drawable;
private float textSize;
public FlexImageButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public FlexImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlexImageButton);
text = a.getText(R.styleable.FlexImageButton_text);
if(text==null){
text="";
}
Drawable d = a.getDrawable(R.styleable.FlexImageButton_src);
if (d != null) {
drawable=d;
} else {
throw new RuntimeException("圖像資源為空");
}
textSize = a.getDimension(R.styleable.FlexImageButton_textSize,12);
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.flex_image_button_layout, this);
imageView = (ImageView) findViewById(R.id.icon_part);
imageView.setImageDrawable(drawable);
textView = (TextView) findViewById(R.id.text_part);
textView.setTextSize((float) textSize);
textView.setText(text);
if(text.equals("")||text==null){
textView.setVisibility(View.GONE);
}
a.recycle();
}
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}
public void setTextViewText(String text) {
textView.setText(text);
}
}
在attrs.xml文件中我們聲明一些自定義屬性,這里我們希望我的FlexImageButton能擁有可以靈活設(shè)置的文字屬性,字體大小屬性、圖標(biāo)資源屬性,因此我這樣定義:
其中format="reference"表示這個(gè)屬性的值類型是資源id,也就是說(shuō)在使用FlexImageButton的時(shí)候我只可以用資源id來(lái)為這個(gè)屬性賦值。屬性值類型有那些,我在文章結(jié)尾的附錄里面一一列出。
上面我們已經(jīng)完成了一個(gè)自定義的控件,activity的布局文件中如下使用FlexImageButton:
android:layout_height="fill_parent"
android:layout_width="50dip"
cl:src="@drawable/toolbar_collect"
cl:text="@string/collect"
android:clickable="true"
android:focusable="true"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:contentDescription="收藏"
android:background="@drawable/back_selector"
/>
仔細(xì)的人會(huì)注意到所有這些屬性中,有兩種形式。其中凡是android:開(kāi)頭的都是系統(tǒng)屬性,而cl:src="@drawable/toolbar_collect"
cl:text="@string/collect"
為我自定義的屬性,為什么是cl:開(kāi)頭?
你也可以不用cl開(kāi)頭,但是不管你用什么開(kāi)頭,如果你用到了自定義屬性,你都必須在activity布局文件的最開(kāi)始這樣聲明:<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cl="http://schemas.android.com/apk/res/com.jcodecraeer.client"
其中cl就是剛剛用到的,com.jcodecraeer.client為我的apk包名,注意是apk包名,而不是你自定義控件的在包中的路徑。
附錄:自定義屬性的值類型:
1. reference:參考某一資源ID。
(1)屬性定義:
(2)屬性使用:
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/圖片ID" />
2. color:顏色值。
(1)屬性定義:
(2)屬性使用:
android:layout_width="42dip"
android:layout_height="42dip"
android:textColor="#00FF00" />
3. boolean:布爾值。
(1)屬性定義:
(2)屬性使用:
android:layout_width="42dip"
android:layout_height="42dip"
android:focusable="true" />
4. dimension:尺寸值。
(1)屬性定義:
(2)屬性使用:
android:layout_width="42dip"
android:layout_height="42dip" />
5. float:浮點(diǎn)值。
(1)屬性定義:
(2)屬性使用:
android:fromAlpha="1.0"
android:toAlpha="0.7" />
6. integer:整型值。
(1)屬性定義:
(2)屬性使用:
android:frameDuration="100"
android:framesCount="12"
/>
7. string:字符串。
(1)屬性定義:
(2)屬性使用:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />
8. fraction:百分?jǐn)?shù)。
(1)屬性定義:
(2)屬性使用:
android:pivotX="200%"
android:pivotY="300%"
/>
9. enum:枚舉值。
(1)屬性定義:
(2)屬性使用:
android:orientation="vertical" >
10. flag:位或運(yùn)算。
(1)屬性定義:
(2)屬性使用:
android:windowSoftInputMode="stateUnspecified | stateUnchanged | stateHidden" >
注意:屬性定義時(shí)可以指定多種類型值:
(1)屬性定義:
(2)屬性使用:
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/圖片ID|#00FF00" />
總結(jié)
以上是生活随笔為你收集整理的android自定义组件属性,android自定义控件并添加属性的方法以及示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c语言两种加法,两个超长正整数的加法
- 下一篇: camera tweak android