自定义属性简单使用

1在values目录下新建attrs文件
2在构造器中通过TypedArray获取属性
3给控件设置属性值
4在代码中使用

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyToggleView">//属性集名称,指定这些属性是属于哪个控件的
        <attr name="circle_color" format="color"/>//声明属性格式
         <attr name="offbackground_color" format="color"/>
        <attr name="onbackground_color" format="color"/>
        <attr name="toggle" format="boolean"></attr>
    </declare-styleable>
    <declare-styleable name="TopBar">
        <attr name="topbartitle" format="string"/>
        <attr name="titleTextSize" format="dimension"/>
        <attr name="topbartitleTextColor" format="color"/>
        <attr name="leftTextColor" format="color"/>
        <attr name="leftBackground" format="color|reference"/>
        <attr name="leftText" format="string"/>
        <attr name="rightTextColor" format="color"/>
        <attr name="rightBackground" format="color|reference"/>
        <attr name="rightText" format="string"/>
    </declare-styleable>
</resources>

所有的format类型
reference 引用
color 颜色
boolean 布尔值
dimension 尺寸值
float 浮点值
integer 整型值
string 字符串
enum 枚举值

package tripcare.qianniu.com.myview.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import tripcare.qianniu.com.myview.R;

/**
 * Created by chenmeng on 2016/11/4.
 */

public class TopBar extends RelativeLayout {
    private  Button mLeftButton;
    private  Button mRightButton;
    private  TextView mTitleView;
    private  LayoutParams mLeftParams;
    private  LayoutParams mRightParams;
    private  LayoutParams mTitleParams;
    private int mLeftTextColor;
    private Drawable mLeftBackground;
    private String mLeftText;
    private int mRightTextColor;
    private Drawable mRightBackground;
    private String mRightText;
    private float mTitleTextSize;
    private int mTitleTextColor;
    private String mTitle;
    private topbarClickListener listener;

    public TopBar(Context context) {
        super(context);
    }

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        mLeftTextColor=ta.getColor(R.styleable.TopBar_leftTextColor,0);
        mLeftBackground=ta.getDrawable(R.styleable.TopBar_leftBackground);
        mLeftText=ta.getString(R.styleable.TopBar_leftText);
        mRightTextColor=ta.getColor(R.styleable.TopBar_rightTextColor,0);
        mRightBackground=ta.getDrawable(R.styleable.TopBar_rightBackground);
        mRightText=ta.getString(R.styleable.TopBar_rightText);
        mTitleTextSize=ta.getDimension(R.styleable.TopBar_titleTextSize,10);
        mTitleTextColor=ta.getColor(R.styleable.TopBar_topbartitleTextColor,0);
        mTitle=ta.getString(R.styleable.TopBar_topbartitle);
        //recycle方法来避免重新创建时的错误
        ta.recycle();
        mLeftButton=new Button(context);
        mRightButton=new Button(context);
        mTitleView = new TextView(context);

        //赋值
        mLeftButton.setTextColor(mLeftTextColor);
        mLeftButton.setBackground(mLeftBackground);
        mLeftButton.setText(mLeftText);

        mRightButton.setTextColor(mRightTextColor);
        mRightButton.setBackground(mRightBackground);
        mRightButton.setText(mRightText);

        mTitleView.setTextColor(mTitleTextColor);
        mTitleView.setText(mTitle);
        mTitleView.setTextSize(mTitleTextSize);
        mTitleView.setGravity(Gravity.CENTER);

        //布局元素
        mLeftParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        addView(mLeftButton,mLeftParams);

        mRightParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(mRightButton,mRightParams);

        mTitleParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(mTitleView,mTitleParams);

        mRightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.reghtClick();

            }
        });

        mLeftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.leftClick();
            }
        });


    }

    public TopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public interface topbarClickListener{
        void leftClick();
        void reghtClick();
    }

    public void setTopbarClickListener(topbarClickListener listener){
        this.listener=listener;
    }
}

在代码中使用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
//安卓Studio命名空间,topbar可任意更改为其他名称
    xmlns:topbar="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
   <tripcare.qianniu.com.myview.widget.TopBar
       android:id="@+id/topbar"
       android:layout_width="match_parent"
       android:layout_height="40dp"
       topbar:leftText="返回"
       topbar:leftBackground="@mipmap/ic_brows_back"
       topbar:leftTextColor="@color/colorPrimary"
       topbar:rightBackground="@mipmap/home"
       topbar:rightText="主页"
       topbar:titleTextSize="12sp"
       topbar:topbartitle="定位"
       topbar:topbartitleTextColor="@color/colorPrimary"></tripcare.qianniu.com.myview.widget.TopBar>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值