TopBar—标题栏自定义View,一行搞定

标题栏自定义布局:

 

<com.xxx.TopBar
        android:id="@+id/top_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

 

private void initTopBar() {
        topBar.setTitile("帮你贷").setVisible(false).setTitleBarListener(new TopBar.BtnClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
    }

     TopBar.java类

public class TopBar extends RelativeLayout implements View.OnClickListener {
    private BtnClickListener listener;

    private ImageView back;
    private TextView titile,right_txt;
    private RelativeLayout bg_color;
    private View view;

    public void setTitleBarListener(BtnClickListener listener) {
        this.listener = listener;
    }

    public interface BtnClickListener {
        void leftClick();

        void rightClick();
    }

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

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


    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.top, this);
        view= (View) findViewById(R.id.view);
        bg_color = (RelativeLayout) findViewById(R.id.bg_color);
        back = (ImageView) findViewById(R.id.back);
        titile = (TextView) findViewById(R.id.titile);
        right_txt= (TextView) findViewById(R.id.right_txt);
        back.setOnClickListener(this);
        right_txt.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back:
                listener.leftClick();
                break;
            case R.id.right_txt:
                listener.rightClick();
                break;

        }
    }


/**
 * 设置背景颜色
 */
public TopBar setTopBackgroundColor(int color){
    bg_color.setBackgroundColor(getResources().getColor(color));//这样才可以
    return this;
}

    /**
     * 设置文字颜色
     */
    public TopBar setTextColor(int color){
        titile.setTextColor(this.getResources().getColor(color));

        return this;
    }
    /**
     * 设置文字
     * @param string
     * @return
     */
    public TopBar setRightTxt(String string){
        right_txt.setText(string);
        return this;
    }

    /**
     * 设置文字
     * @param string
     * @return
     */
    public TopBar setTitile(String string){
        titile.setText(string);
        return this;
    }
    /**
     * 编辑框是否可见
     */
    public TopBar setVisible(Boolean flag){
        if (flag) {
            back.setVisibility(GONE);
        } else {
            back.setVisibility(VISIBLE);
        }
        return this;
    }
    /**
     * 编辑框是否可见
     */
    public TopBar setVisibleView(Boolean flag){
        if (flag) {
            view.setVisibility(GONE);
        } else {
            view.setVisibility(VISIBLE);
        }
        return this;
    }
}

   top.xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bg_color"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@android:color/white"
    >

    <ImageView
       android:padding="5dp"
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/back"
        android:gravity="center"
        android:textSize="16sp"
        android:layout_above="@+id/view"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <TextView
        android:id="@+id/titile"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="#555555"
        android:textSize="16sp" />
    <TextView
        android:id="@+id/right_txt"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="#555555"
        android:textSize="14sp" />
    <View
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="#c2c2c2"
        android:id="@+id/view" />

</RelativeLayout>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值