当前位置: 移动技术网 > IT编程>移动开发>Android > android 自定义控件 自定义属性详细介绍

android 自定义控件 自定义属性详细介绍

2019年07月24日  | 移动技术网IT编程  | 我要评论

徐僧老婆,苗神客,陈绍宽

自定义控件在android中无处不见,自定义控件给了我们很大的方便。比如说,一个视图为imageview ,imagebutton ,textview 等诸多控件的组合,用的地方有很多,我们不可能每次都来写3个的组合,既浪费时间,效率又低。在这种情况下,我们就可以自定义一个view来替换他们,不仅提升了效率并且在xml中运用也是相当的美观。
一、控件自定义属性介绍
以下示例中代码均在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;
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网