笔者之前已经写过了一些自定义View的文章,在此对其也就不从头说起了,如有兴趣的读者可以看一下笔者的前两篇文章。
android 自定义view的使用(最佳demo——返回标题栏)
android 自定义控件(底部icon点击效果)
笔者之前的文章中仅仅介绍了如何使用自定义View以及为什么要使用自定义View等等,但是在实际操作中,我们还是希望自定义View之后,直接能够在xml中就对其进行操作,如下图:
那么如何操作呢?主要是三个步骤:
1、自定义属性名称
2、将属性名称与控件关联
3、从第三方命名空间获取到自定义属性名称
主要代码:
1、自定义属性名称
首先要在values文件中创建一个xml文件,并且在其中写上你需要的自定义属性的名称以及类型。
atts.xml中代码如下:
<?xml version="1.0" encoding="utf-8"?>@H_502_32@
<resources@H_502_32@>@H_502_32@
<declare-styleable@H_502_32@ name@H_502_32@="MyTitle"@H_502_32@>@H_502_32@
<attr@H_502_32@ name@H_502_32@="textColor"@H_502_32@ format@H_502_32@="color"@H_502_32@/>@H_502_32@
<attr@H_502_32@ name@H_502_32@="titleText"@H_502_32@ format@H_502_32@="string"@H_502_32@/>@H_502_32@
<attr@H_502_32@ name@H_502_32@="leftText"@H_502_32@ format@H_502_32@="string"@H_502_32@/>@H_502_32@
<attr@H_502_32@ name@H_502_32@="rightText"@H_502_32@ format@H_502_32@="string"@H_502_32@/>@H_502_32@
</declare-styleable@H_502_32@>@H_502_32@
</resources@H_502_32@>@H_502_32@
2、将属性名称与控件关联
此点比较简单,直接看代码:
MyView.java
package@H_502_32@ com.example.double2.viewxmltest;
import@H_502_32@ android.content.Context;
import@H_502_32@ android.content.res.TypedArray;
import@H_502_32@ android.graphics.Color;
import@H_502_32@ android.util.AttributeSet;
import@H_502_32@ android.view.LayoutInflater;
import@H_502_32@ android.widget.LinearLayout;
import@H_502_32@ android.widget.TextView;
/** * 项目名称:ViewXmlTest * 创建人:Double2号 * 创建时间:2016/8/4 10:23 * 修改备注: */@H_502_32@
public@H_502_32@ class@H_502_32@ MyView@H_502_32@ extends@H_502_32@ LinearLayout@H_502_32@ {@H_502_32@
private@H_502_32@ int@H_502_32@ colorText;
private@H_502_32@ String textLeft;
private@H_502_32@ String textTitle;
private@H_502_32@ String textRight;
private@H_502_32@ TextView tvLeft;
private@H_502_32@ TextView tvTitle;
private@H_502_32@ TextView tvRight;
public@H_502_32@ MyView@H_502_32@(Context context,AttributeSet attrs,int@H_502_32@ defStyleAttr) {
super@H_502_32@(context,attrs,defStyleAttr);
//从xml的属性中获取到字体颜色与string@H_502_32@
TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.MyTitle);
colorText=ta.getColor(R.styleable.MyTitle_textColor,Color.BLACK);
textLeft=ta.getString(R.styleable.MyTitle_leftText);
textTitle=ta.getString(R.styleable.MyTitle_titleText);
textRight=ta.getString(R.styleable.MyTitle_rightText);
ta.recycle();
//获取到控件@H_502_32@
//加载布局文件,与setContentView()效果一样@H_502_32@
LayoutInflater.from(context).inflate(R.layout.my_view,this@H_502_32@);
tvLeft=(TextView)findViewById(R.id.tv_left);
tvTitle=(TextView)findViewById(R.id.tv_title);
tvRight=(TextView)findViewById(R.id.tv_right);
//将控件与设置的xml属性关联@H_502_32@
tvLeft.setTextColor(colorText);
tvLeft.setText(textLeft);
tvTitle.setTextColor(colorText);
tvTitle.setText(textTitle);
tvRight.setTextColor(colorText);
tvRight.setText(textRight);
}
}
my_view.xml
<?xml version="1.0" encoding="utf-8"?>@H_502_32@
<LinearLayout@H_502_32@ xmlns:android@H_502_32@="http://schemas.android.com/apk/res/android"@H_502_32@ xmlns:tools@H_502_32@="http://schemas.android.com/tools"@H_502_32@ android:layout_width@H_502_32@="match_parent"@H_502_32@ android:layout_height@H_502_32@="wrap_content"@H_502_32@ android:orientation@H_502_32@="horizontal"@H_502_32@ android:padding@H_502_32@="10dp"@H_502_32@ tools:background@H_502_32@="@android:color/holo_blue_dark"@H_502_32@>@H_502_32@
<TextView @H_502_32@ android:id@H_502_32@="@+id/tv_left"@H_502_32@ android:layout_width@H_502_32@="wrap_content"@H_502_32@ android:layout_height@H_502_32@="wrap_content"@H_502_32@ android:textSize@H_502_32@="18sp"@H_502_32@ tools:text@H_502_32@="left"@H_502_32@ tools:textColor@H_502_32@="#fff"@H_502_32@/>@H_502_32@
<TextView @H_502_32@ android:id@H_502_32@="@+id/tv_title"@H_502_32@ android:layout_width@H_502_32@="0dp"@H_502_32@ android:layout_height@H_502_32@="wrap_content"@H_502_32@ android:layout_weight@H_502_32@="1"@H_502_32@ android:gravity@H_502_32@="center"@H_502_32@ android:textSize@H_502_32@="23sp"@H_502_32@ tools:text@H_502_32@="title"@H_502_32@ tools:textColor@H_502_32@="#fff"@H_502_32@/>@H_502_32@
<TextView @H_502_32@ android:id@H_502_32@="@+id/tv_right"@H_502_32@ android:layout_width@H_502_32@="wrap_content"@H_502_32@ android:layout_height@H_502_32@="wrap_content"@H_502_32@ android:textSize@H_502_32@="18sp"@H_502_32@ tools:text@H_502_32@="right"@H_502_32@ tools:textColor@H_502_32@="#fff"@H_502_32@/>@H_502_32@
</LinearLayout@H_502_32@>@H_502_32@
3、从第三方命名空间获取到自定义属性名称
此处要注意在activity_main.xml要申明第三方命名空间(在android studio中只需要用res-auto,在eclipse中就需要加上完整的包名,如下图)
注:my_view只是使用时的一个名称而已,后方的“http://schemas.android.com/apk/res-auto”才是真正有用的。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>@H_502_32@
<RelativeLayout @H_502_32@ xmlns:android@H_502_32@="http://schemas.android.com/apk/res/android"@H_502_32@ xmlns:my_view@H_502_32@="http://schemas.android.com/apk/res-auto"@H_502_32@ android:layout_width@H_502_32@="match_parent"@H_502_32@ android:layout_height@H_502_32@="match_parent"@H_502_32@ >@H_502_32@
<com.example.double2.viewxmltest.MyView @H_502_32@ android:layout_width@H_502_32@="match_parent"@H_502_32@ android:layout_height@H_502_32@="wrap_content"@H_502_32@ android:background@H_502_32@="@android:color/holo_blue_dark"@H_502_32@ my_view:leftText@H_502_32@="Back"@H_502_32@ my_view:rightText@H_502_32@="Go"@H_502_32@ my_view:textColor@H_502_32@="#fff"@H_502_32@ my_view:titleText@H_502_32@="MyViewTest"@H_502_32@ />@H_502_32@
</RelativeLayout@H_502_32@>@H_502_32@