一个简单的自定义TopBar

在看《Android群英传》看到自定义view复合控件,就照着书敲了下。

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTopBar">
        <attr name="mtitle" format="string"/>
        <attr name="mtitleTextSize" format="dimension"/>
        <attr name="mtitleTextColor" format="color"/>
        <attr name="leftTextColor" format="color"/>
        <attr name="leftBackground" format="reference|color"/>
        <attr name="leftText" format="string"/>
        <attr name="rightTextColor" format="color"/>
        <attr name="rightBackground" format="reference|color"/>
        <attr name="rightText" format="string"/>
    </declare-styleable>

</resources>

MyTopBar.java

public class MyTopBar extends RelativeLayout {

    private TypedArray typedArray;
    private int titleTextColor;
    private int leftTextColor;
    private int rightTextColor;
    private Drawable leftBackground;
    private Drawable rightBackground;
    private String rightText;
    private String leftText;
    private String title;
    private float titleTextSize;
    private Button mLeftButton;
    private Button mRightButton;
    private TextView mTitleView;
    private LayoutParams mLeftParams;
    private LayoutParams mRightParams;
    private LayoutParams mTitlepParams;
    topbarClickListener listener;

    public MyTopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获取属性
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTopBar);
        titleTextColor = typedArray.getColor(R.styleable.MyTopBar_mtitleTextColor, 0);
        leftTextColor = typedArray.getColor(R.styleable.MyTopBar_leftTextColor, 0);
        rightTextColor = typedArray.getColor(R.styleable.MyTopBar_rightTextColor, 0);
        leftBackground = typedArray.getDrawable(R.styleable.MyTopBar_leftBackground);
        rightBackground = typedArray.getDrawable(R.styleable.MyTopBar_rightBackground);
        leftText = typedArray.getString(R.styleable.MyTopBar_leftText);
        rightText = typedArray.getString(R.styleable.MyTopBar_rightText);
        title = typedArray.getString(R.styleable.MyTopBar_mtitle);
        titleTextSize = typedArray.getDimension(R.styleable.MyTopBar_mtitleTextSize, 10);
        typedArray.recycle();


        mLeftButton = new Button(context);
        mRightButton = new Button(context);
        mTitleView = new TextView(context);

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

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

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

        mLeftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        addView(mLeftButton, mLeftParams);

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

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


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

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

    }

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

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

            }
        }
    }

    public interface topbarClickListener {

        void leftClick();

        void rightClick();
    }


}

使用activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <com.example.lklknbnhh.mytopbar.MyTopBar
        android:background="@color/colorPrimaryDark"
        android:id="@+id/topbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        custom:leftText="返回"
        custom:leftTextColor="#ffffff"
        custom:rightText="更多"
        custom:rightTextColor="#ffffff"
        custom:rightBackground="@mipmap/ic_launcher"
        custom:mtitle="我是标题"
        custom:mtitleTextColor="#ffffff"
        custom:mtitleTextSize="10sp"
        />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    private MyTopBar topBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        topBar = (MyTopBar)findViewById(R.id.topbar);
        topBar.setOnTopBarClickListener(new MyTopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                Toast.makeText(getApplicationContext(),"左侧按钮被点击",Toast.LENGTH_SHORT).show();
                topBar.setButtonVisable(0, false);
                topBar.setButtonVisable(1,true);
            }

            @Override
            public void rightClick() {
                Toast.makeText(getApplicationContext(),"右侧按钮被点击",Toast.LENGTH_SHORT).show();
                topBar.setButtonVisable(1,false);
                topBar.setButtonVisable(0,true);
            }
        });
    }
}

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值