标题栏

这是attrs.xml 文件需要定义的
<declare-styleable name="FanTopBar">
    <attr name="fany_leftBackground" format="reference"/>
    <attr name="fany_rightBackground" format="reference"/>
    <attr name="fany_titleText" format="string"/>
    <attr name="fany_titleTextSize" format="dimension"/>
    <attr name="fany_titleTextColor" format="color"/>

</declare-styleable>



这是类里面写 

public class BiaotiLan extends RelativeLayout {


    private Button leftButton;
    private Button rigtButton;
    private TextView titleTextView;
    private TypedArray typedArray;

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

    public BiaotiLan(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.biaoti,this);
        leftButton = findViewById(R.id.btn_fan);

        rigtButton = findViewById(R.id.btn_duo);

        titleTextView = findViewById(R.id.t1);


        //获得自定义属性并赋值
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.FanTopBar);

        int leftBtnBackground = typedArray.getResourceId(R.styleable.FanTopBar_fany_leftBackground, 0);
        int rightBtnBackgount = typedArray.getResourceId(R.styleable.FanTopBar_fany_rightBackground, 0);
        String titleText = typedArray.getString(R.styleable.FanTopBar_fany_titleText);
        float titleTextSize = typedArray.getDimension(R.styleable.FanTopBar_fany_titleTextSize, 0);
        int titleTextColor = typedArray.getColor(R.styleable.FanTopBar_fany_titleTextColor, 0x38ad5a);


        //释放资源
        typedArray.recycle();
        leftButton.setBackgroundResource(leftBtnBackground);
        rigtButton.setBackgroundResource(rightBtnBackgount);
        titleTextView.setText(titleText);
        titleTextView.setTextSize(titleTextSize);
        titleTextView.setTextColor(titleTextColor);



    }

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



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值