android开发中自定义View的好处是显而易见的。比如说下面的这个顶部导航,它被设计出现在应用的每个界面,但每次的内容却不尽相同。我们不能在每个layout资源中都配置一组相同的View吧?如果使用<include layou="@layout/xxx"/>标签,虽然解决了布局文件的重用性,但是相关View的初始化设置还是没能够重用(集中),需要每次都采用view.findViewById(id)来初始化他们。
有了对“可重用性”的考量,我们来完成一次对自定义View的探索。
这里创建的布局文件和平日里为Activity或Fragment创建的布局文件没有区别,例如下面的xml创建了一个上面是图片,下面是文字的自定义View布局:
<?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" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ic_launcher" />
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="asdfasdf" />
</LinearLayout>
package org.xiaom.customView.view;
import org.xiaom.customView.R;
public class MyView extends LinearLayout {
private View root = null;
//上面的img
private ImageView imgView = null;
//img下面的text
private TextView txtView = null;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public MyView(Context context) {
super(context);
initView(context);
}
private void initView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
root = inflater.inflate(R.layout.view_my_view, this,true);
imgView = (ImageView) root.findViewById(R.id.img);
txtView = (TextView) root.findViewById(R.id.txt);
}
}
在完成上面的自定义操作后,接下来就可以像使用android原生组件那样使用我们的自定义View了。需要注意的是,自定义View在xml中声明使用时,必须采用全类名(类全名- -)。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 类全名 -->
<org.xiaom.customView.view.MyView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="aasdfasdfasdfa" />
</LinearLayout>
接下来就很简单了,我们直接使用Activity.this.setContentView(layouId)方法即可。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="myView">
<attr name="img" format="reference" />
<attr name="text" format="string"></attr>
</declare-styleable>
</resources>package org.xiaom.customView.view;
import org.xiaom.customView.R;
public class MyView extends LinearLayout {
private View root = null;
// 上面的img
private ImageView imgView = null;
// img下面的text
private TextView txtView = null;
// 上面的图像资源Id
Drawable img;
// 文字内容
String text;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.myView);
img = ta.getDrawable(R.styleable.myView_img);
text = ta.getString(R.styleable.myView_text);
initView(context);
//记得此处要recycle();
ta.recycle();
}
private void initView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
root = inflater.inflate(R.layout.view_my_view, this, true);
imgView = (ImageView) root.findViewById(R.id.img);
txtView = (TextView) root.findViewById(R.id.txt);
//将自定义属性的值传递给对应View
imgView.setBackgroundResource(R.drawable.icon_consultation);
txtView.setText(text);
}
}
下面的xml表明如何在xml中配置自定义属性:<!-- 我的adt版本是v22.6.2-1085508,它支持自动找寻和验证declare-styleable;如果你的adt不支持此功能,请换成 -->
<!-- xmlns:myView="http://schemas.android.com/apk/org.xiaom.customView.myView",此处我没验证 -->
<LinearLayout xmlns:myView="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 类全名 -->
<org.xiaom.customView.view.MyView
android:layout_width="match_parent"
android:layout_height="wrap_content"
myView:img="@drawable/icon_consultation"
myView:text="自定义Text" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="aasdfasdfasdfa" />
</LinearLayout>好,大功告成。这里是本博文实例的Eclipse工程。Android自定义控件并且使其可以在xml中自定义属性,布布扣,bubuko.com
原文:http://blog.csdn.net/mthhk008/article/details/30070119