自定义View(TopBar)

1、在values下面创建一个atts.xml文件夹

<attr name="title1" format="string"/>
<attr name="titleSize1" format="dimension"/>
<attr name="titleColor1" format="color"/>

<attr name="leftText1" format="string"/>
<attr name="leftTextColor1" format="color"/>
<attr name="leftTextBackground1" format="reference|color"/>

<attr name="rightText1" format="string"/>
<attr name="rightTextColor1" format="color"/>
<attr name="rightTextBackground1" format="reference|color"/>

2、创建一个类继承RelativeLayoyt,先实例化里面的属性值


private Button leftButton;
private Button rightButton;
private TextView text;

private String title;
private float textSize;
private int textColor;

private String leftText;
private Drawable leftBackground;
private int leftTextColor;

private String rightText;
private Drawable rightBackground;
private int rightTextColor;

private LayoutParams leftParams, rightParams,titleParame;

3、实现里面的一个有两个参数的构造方法

public topBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.top);

    title=ta.getString(R.styleable.top_title1);
    textSize=ta.getDimension(R.styleable.top_titleSize1,0);
    textColor=ta.getColor(R.styleable.top_titleColor1,0);

    leftText=ta.getString(R.styleable.top_leftText1);
    leftBackground=ta.getDrawable(R.styleable.top_leftTextBackground1);
    leftTextColor=ta.getColor(R.styleable.top_leftTextColor1,0);

     rightText=ta.getString(R.styleable.top_rightText1);
     rightBackground=ta.getDrawable(R.styleable.top_rightTextBackground1);
     rightTextColor=ta.getColor(R.styleable.top_rightTextColor1,0);

     ta.recycle();

4、实例化自定义控件

//实例自定义化控件
 leftButton=new Button(context);
 rightButton=new Button(context);
 text=new TextView(context);
5、给控件设置值

//给控件设置属性值
text.setText(title);
text.setTextSize(textSize);
text.setTextColor(textColor);

leftButton.setText(leftText);
leftButton.setTextColor(leftTextColor);
leftButton.setBackground(leftBackground);

rightButton.setText(rightText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackground);

setBackgroundColor(0xFFF59563);
6、设置控件设置宽高属性

leftParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftButton,leftParams);

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

titleParame=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
titleParame.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(text,titleParame);
7、接口回调

private topBarClientlisten listen;
public interface topBarClientlisten{
    public void leftClient();
    public void rightClient();
}
public void TopBarClientListen(topBarClientlisten listen){
    this.listen=listen;
}
  leftButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            listen.leftClient();
        }
    });
    rightButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            listen.rightClient();
        }
    });
}
public void setVisibaleView(boolean flag){
    if(flag){
       leftButton.setVisibility(View.VISIBLE);
    }else{
        leftButton.setVisibility(View.GONE);
    }
}
Main方法

topBar top= (topBar) findViewById(R.id.topbar);
top.TopBarClientListen(new topBar.topBarClientlisten() {
    @Override
    public void leftClient() {
        Intent intent=new Intent(MainActivity.this,TwoActivity.class);
        startActivity(intent);
    }

    @Override
    public void rightClient() {
        Toast.makeText(MainActivity.this,"MROE",Toast.LENGTH_SHORT).show();
    }
});
top.setVisibaleView(false);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值