1.自定义属性,可在XML文件中直接使用:
在res下values文件新建 attrs.xml ,然后写你想要的自定义属性,比如
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomNewToolBar">
<attr name="titleText" format="string|reference" />
<attr name="myTitleTextColor" format="color|reference" />
<attr name="titleTextSize" format="dimension|reference" />
<attr name="leftImageSrc" format="reference" />
<attr name="rightImageSrc" format="reference" />
</declare-styleable>
</resources>
2.自定义Java 类(重点):
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.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class CustomNewToolBar extends RelativeLayout {
private ImageView leftImg,rightImg;
private TextView mTitle;
public CustomNewToolBar(Context context) {
super(context);
}
public CustomNewToolBar(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context,attrs);
}
//声明一个接口
public interface ImgOnclick{
void leftOnclick();
void rightOnclick();
}
//声明接口变量
private ImgOnclick mImgOnclick;
//为接口变量设置一个set方法
public void setImgOnclick(ImgOnclick imgOnclick){
this.mImgOnclick = imgOnclick;
}
private void initView(Context context,AttributeSet attrs) {
View.inflate(context,R.layout.custom_view,this);
leftImg = findViewById(R.id.img_left);
rightImg = findViewById(R.id.img_right);
mTitle = findViewById(R.id.title);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomNewToolBar);
String titleText = ta.getString(R.styleable.CustomNewToolBar_titleText);
//第二个参数表示默认颜色
int titleTextColor = ta.getColor(R.styleable.CustomNewToolBar_myTitleTextColor, Color.BLACK);
//已经由sp转为px
float titleTextSize = ta.getDimension(R.styleable.CustomNewToolBar_titleTextSize, 12);
//读取图片
Drawable leftDrawable = ta.getDrawable(R.styleable.CustomNewToolBar_leftImageSrc);
Drawable rightDrawable = ta.getDrawable(R.styleable.CustomNewToolBar_rightImageSrc);
leftImg.setImageDrawable(leftDrawable);
rightImg.setImageDrawable(rightDrawable);
mTitle.setText(titleText);
mTitle.setTextColor(titleTextColor);
mTitle.setTextSize(titleTextSize);
//如果XML中没有加载这个属性,就需要隐藏,否则点击事件同样有效
if (null == leftDrawable ){
leftImg.setVisibility(GONE);
}
if (null == rightDrawable ){
rightImg.setVisibility(GONE);
}
leftImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != mImgOnclick){
mImgOnclick.leftOnclick();
}
}
});
rightImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != mImgOnclick){
mImgOnclick.rightOnclick();
}
}
});
}
public CustomNewToolBar setTitle(String title){
if (null != mTitle){
mTitle.setText(title);
}
//链式调用的关键
return this;
}
public CustomNewToolBar setTitleSize(float in){
if (null != mTitle){
mTitle.setTextSize(in);
}
return this;
}
}
3.在XML中使用你想要的属性
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.cris.customview.CustomNewToolBar
android:id="@+id/custom"
app:titleText="2222"
app:leftImageSrc="@mipmap/left"
app:rightImageSrc="@mipmap/right"
app:myTitleTextColor="@color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
4.你也可以在代码中设置相关属性,前提是Java代码中有相关方法:
CustomNewToolBar customNewToolBar =findViewById(R.id.custom);
customNewToolBar.setTitle("标题");
customNewToolBar.setTitleSize(20);
customNewToolBar.setImgOnclick(new CustomNewToolBar.ImgOnclick() {
@Override
public void leftOnclick() {
Toast.makeText(MainActivity.this,"点击左边img",Toast.LENGTH_SHORT).show();
}
@Override
public void rightOnclick() {
Toast.makeText(MainActivity.this,"点击右边img",Toast.LENGTH_SHORT).show();
}
});
自定义 View 的几个知识点,要自定义一个控件主要包含几个方法。
onMeasure:测量View的宽高;
onLayout:计算当前View以及子View的位置;
onDraw:视图的绘制工作;