<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomXmlTopBar">
<!--topbar中间文字属性-->
<attr name="titleText" format="string"/>
<attr name="titleTextColor" format="color"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="lefttitleText" format="string"/>
<attr name="lefttitleTextColor" format="color"/>
<attr name="lefttitleTextSize" format="dimension"/>
<!--topbar左边控件属性-->
<attr name="leftSrc" format="reference"/>
<!--topbar左边控件属性-->
<attr name="rightSrc" format="reference"/>
</declare-styleable>
</resources>
package com.bwei.Test.zhangjian20170919lianxi.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.bwei.Test.zhangjian20170919lianxi.R;
/**
* @author ${张健}
* @date 2017/9/20/16:36
*/
public class CustomXmlTopBar extends FrameLayout {
//定义控件变量
private ImageView iv_topbar_right;
private TextView tv_topbar_titletext,iv_topbar_left;
//属性变量
private String titleText,lefttitleText;
private int titleTextColor,lefttitleTextColor;
private float titleTextSize,lefttitleTextSize;
// private Drawable leftSrc;
private Drawable rightSrc;
public CustomXmlTopBar(Context context) {
super(context);
}
public CustomXmlTopBar(Context context, AttributeSet attrs) {
super(context, attrs);
//填充布局
LayoutInflater.from(context).inflate(R.layout.title,this);
iv_topbar_left = (TextView) findViewById(R.id.iv_topbar_left);
iv_topbar_right = (ImageView) findViewById(R.id.iv_topbar_right);
tv_topbar_titletext = (TextView) findViewById(R.id.tv_topbar_titletext);
//获取自定义属性数组
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomXmlTopBar);
//获取中间属性
titleText = typedArray.getString(R.styleable.CustomXmlTopBar_titleText);
titleTextColor = typedArray.getColor(R.styleable.CustomXmlTopBar_titleTextColor,000000);
titleTextSize = typedArray.getDimension(R.styleable.CustomXmlTopBar_titleTextSize,20);
//获取左边文字属性
lefttitleText = typedArray.getString(R.styleable.CustomXmlTopBar_lefttitleText);
lefttitleTextColor = typedArray.getColor(R.styleable.CustomXmlTopBar_lefttitleTextColor,000000);
lefttitleTextSize = typedArray.getDimension(R.styleable.CustomXmlTopBar_lefttitleTextSize,20);
// leftSrc = typedArray.getDrawable(R.styleable.CustomXmlTopBar_leftSrc);
rightSrc = typedArray.getDrawable(R.styleable.CustomXmlTopBar_rightSrc);
typedArray.recycle();
//设置属性
iv_topbar_left.setText(lefttitleText);
iv_topbar_left.setTextColor(lefttitleTextColor);
iv_topbar_left.setTextSize(lefttitleTextSize);
//右边图片
iv_topbar_right.setImageDrawable(rightSrc);
//中间文字
tv_topbar_titletext.setText(titleText);
tv_topbar_titletext.setTextColor(titleTextColor);
tv_topbar_titletext.setTextSize(titleTextSize);
}
public CustomXmlTopBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
// @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
// public CustomXmlTopBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
// super(context, attrs, defStyleAttr, defStyleRes);
// }
//
// //设置右控件是否显示,默认为显示
// public void setRightIsvisable(boolean isvisableFlag){
// if (isvisableFlag){
// iv_topbar_right.setVisibility(View.VISIBLE);
// }else {
// iv_topbar_right.setVisibility(View.GONE);
// }
// }
}
<com.bwei.Test.zhangjian20170919lianxi.view.CustomXmlTopBar
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="50dp"
app:lefttitleText="全部"
app:lefttitleTextColor="#000"
app:titleText="首页"
app:titleTextColor="#fff"
app:rightSrc="@drawable/dynamic_titlebar_normal"
>
</com.bwei.Test.zhangjian20170919lianxi.view.CustomXmlTopBar>