一个简单的自定义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);
            }
        });
    }
}

一个简单的自定义TopBar_第1张图片

你可能感兴趣的:(android,控件)