Android 自定义ToolBar

android 自定义ToolBar

首先自定义属性

 
        
        
        
        
        
        
        
        
        
            
            
            
        
        
        
    

自定义View集成Linalayout
custom_toolbar布局文件



    
    
        
    
        
    
        
            
    
    
    

package com.example.administrator.livedemo;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 自定义头布局
 */
public class CustomToolBar extends LinearLayout implements View.OnClickListener {
    private Context mContext;
    private ImageView mIvBack;
    private TextView mTvTitle;
    private ImageView mIvRight;
    private TextView mTvRight;
    private View mLine;

    public CustomToolBar(Context context) {
        super(context);
        this.mContext = context;
        initView(context, null, 0);
    }

    public CustomToolBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView(context, attrs, 0);
    }

    public CustomToolBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        LayoutInflater.from(context).inflate(R.layout.custom_toolbar, this);

        mIvBack = findViewById(R.id.iv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mTvRight = findViewById(R.id.tv_right);
        mIvRight = findViewById(R.id.iv_right);
        mLine = findViewById(R.id.line);
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomToolBar);
            int title_text_color = typedArray.getColor(R.styleable.CustomToolBar_title_text_color, 0xff000000);
            String title_text = typedArray.getString(R.styleable.CustomToolBar_title_text);
            float title_size = typedArray.getDimension(R.styleable.CustomToolBar_title_text_size, 18);
            int back_src = typedArray.getResourceId(R.styleable.CustomToolBar_back_src, 0);
            int right_src = typedArray.getResourceId(R.styleable.CustomToolBar_right_src, 0);
            int right_title_v = typedArray.getInteger(R.styleable.CustomToolBar_right_title_visibility, View.VISIBLE);
            String right_title = typedArray.getString(R.styleable.CustomToolBar_right_title);
            int right_title_color = typedArray.getColor(R.styleable.CustomToolBar_right_title_color, 0xff000000);
            float right_title_size = typedArray.getDimension(R.styleable.CustomToolBar_title_text_size, 16);
            float line_height = typedArray.getDimension(R.styleable.CustomToolBar_line_height, 1);
            int line_color = typedArray.getColor(R.styleable.CustomToolBar_line_color, 0x00000000);
            mIvBack.setImageResource(back_src);
            mTvTitle.setText(title_text);
            mTvTitle.setTextColor(title_text_color);
            mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, title_size);
            mTvRight.setText(right_title);
            mTvRight.setTextColor(right_title_color);
            mTvRight.setTextSize(TypedValue.COMPLEX_UNIT_PX, right_title_size);
            mIvRight.setImageResource(right_src);
            mTvRight.setVisibility(right_title_v);
            mLine.setBackgroundColor(line_color);
            ViewGroup.LayoutParams layoutParams = mLine.getLayoutParams();
            layoutParams.height = (int) line_height;
            mLine.setLayoutParams(layoutParams);
            typedArray.recycle();
        }
    }
@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                ((Activity) mContext).finish();
                break;

            case R.id.rl_right:
                if (onRightClickListener != null) {
                    onRightClickListener.onRight();
                }
                break;

        }
    }

    public interface OnRightClickListener {
        void onRight();
    }

    private OnRightClickListener onRightClickListener;


    public void setOnRightClickListener(OnRightClickListener onRightClickListener) {
        this.onRightClickListener = onRightClickListener;
    }
}

布局文件中使用

 

好了 完成 简单的一个ToolBar完成了

很简单 希望能够帮助到大家 第一次写博客写的不好请留言 写下 好的建议!!

你可能感兴趣的:(Android)