一、控件自定义属性介绍
以下示例中代码均在values/attrs.xml 中定义,属性均可随意命名。
1. reference:参考某一资源ID。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
<attr name = "src" format = "reference" />
</declare-styleable>
2. color:颜色值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "textcolor" format = "color" />
</declare-styleable>
3. boolean:布尔值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
4. dimension:尺寸值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "layout_wIDth" format = "dimension" />
</declare-styleable>
5. float:浮点值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
6. integer:整型值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
</declare-styleable>
7. string:字符串。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "text" format = "string" />
</declare-styleable>
8. fraction:百分数。
示例:
[java]
复制代码 代码如下:
<declare-styleable name="名称">
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
</declare-styleable>
9. enum:枚举值。
示例:
[java]
复制代码 代码如下:
<declare-styleable name="名称">
<attr name="orIEntation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
10. flag:位或运算。
示例:
[java]
复制代码 代码如下:
<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.多类型。
示例:
[java]
复制代码 代码如下:
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
-------------------------------------------------------------------------------------------
二、属性的使用以及自定义控件的实现
1、构思控件的组成元素,思考所需自定义的属性。
比如:我要做一个 <带阴影的按钮,按钮正下方有文字说明>(类似9宫格按钮)
新建values/attrs.xml
[java]
复制代码 代码如下:
<?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为字体大小。
2、怎么自定义控件呢,怎么使用这些属性呢?话不多说请看代码,CustomVIEw :
复制代码 代码如下:
package com.nanlus.custom;
import com.nanlus.custom.R;
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);
}
}
代码很简单,就不多说,下面来看看我们的CustomVIEw是怎么用的,请看:
3、自定义控件的使用
话不多说,请看代码,main.xml:
[java]
复制代码 代码如下:
<?xml version="1.0" enCoding="utf-8"?>
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"
xmlns:nanlus="http://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>
在这里需要解释一下,
xmlns:nanlus="http://schemas.androID.com/apk/res/com.nanlus.custom"
nanlus为在xml中的前缀,com.nanlus.custom为包名
4、在Activity中,直接上代码
[java]
复制代码 代码如下:
package com.nanlus.custom;
import androID.os.Bundle;
import androID.vIEw.VIEw;
import androID.Widget.Imagebutton;
import androID.Widget.ImageVIEw;
import androID.Widget.TextVIEw;
import androID.Widget.Toast;
import com.nanlus.BaseActivity;
import com.nanlus.custom.R;
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;
}
}
}
总结
以上是内存溢出为你收集整理的android 自定义控件 自定义属性详细介绍全部内容,希望文章能够帮你解决android 自定义控件 自定义属性详细介绍所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)