工具类/自定义顶栏

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

/**
 * Created by YRC on 2017/4/20.
 */

public class TopBar extends RelativeLayout{
    String title,leftText,rightText;
    int titleTextColor, leftTextColor, rightTextColor;
    float titleTextSize;
    Drawable leftBackground, rightBackground;
    Button leftButton,rightButton;
    TextView titleView;
    LayoutParams rightParams,leftParams,titleParams;
    topbarClickListener mListener;

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //通过这个方法,将你在attrs.xml中定义的declare-styleable的所有属性的值存储到TypedArray        TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.TopBar);
        //TypedArray中取出对应的值赖伟要设置的属性赋值
        title=ta.getString(R.styleable.TopBar_title);
        leftText=ta.getString(R.styleable.TopBar_leftText);
        rightText=ta.getString(R.styleable.TopBar_rightText);
        titleTextColor=ta.getColor(R.styleable.TopBar_titleTextColor,0);
        leftTextColor=ta.getColor(R.styleable.TopBar_leftTextColor,0);
        rightTextColor=ta.getColor(R.styleable.TopBar_rightTextColor,0);
        titleTextSize=ta.getDimension(R.styleable.TopBar_titleTextSize,10);
        leftBackground=ta.getDrawable(R.styleable.TopBar_leftBackground);
        rightBackground=ta.getDrawable(R.styleable.TopBar_rightBackground);
        //获取完TypedArray的值后,一般要调用recyle方法来避免重新创建的时候的错误。完成资源的回收
        ta.recycle();

        leftButton=new Button(context);
        rightButton=new Button(context);
        titleView=new TextView(context);

        //为创建的组件元素赋值,值就来源于我们在引用的xml文件中给对象属性的赋值。
        leftButton.setText(leftText);
        leftButton.setTextColor(leftTextColor);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            leftButton.setBackground(leftBackground);
        }

        rightButton.setText(rightText);
        rightButton.setTextColor(rightTextColor);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            rightButton.setBackground(rightBackground);
        }

        titleView.setText(title);
        titleView.setTextColor(titleTextColor);
        titleView.setTextSize(titleTextSize);
        titleView.setGravity(Gravity.CENTER);

        //为组件元素设置相应的布局元素
        leftParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        addView(leftButton,leftParams);

        rightParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(rightButton,rightParams);

        titleParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(titleView,titleParams);

        rightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.rightClick();
            }
        });
        leftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.leftClick();
            }
        });
    }

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

    public interface topbarClickListener{
        void leftClick();

        void rightClick();
    }

    //暴露一个方法给调用者来注册接口回调
    //通过接口来获得回调者对接口方法的实现

    public void  setOnTopBarClickListener(topbarClickListener mListener){
        this.mListener=mListener;
    }

    public void setButtonVisable(int id,boolean flag){
        if (flag){
            if (id==0){
                leftButton.setVisibility(View.VISIBLE);
            }else {
                rightButton.setVisibility(View.VISIBLE);
            }
        }else {
            if (id==0){
                leftButton.setVisibility(View.GONE);
            }else {
                rightButton.setVisibility(View.GONE);
            }
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值