自定义Android标题栏

预览效果

这里写图片描述

在value中添加resource文件

自定义Android标题栏_第1张图片

建立相关类来实现相关的属性

package com.plf.weknow.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.plf.weknow.R;

/**
 * Created by Peng on 2017/3/3.
 */
public class MyTopbar extends LinearLayout{

    //包含的控件
    private TextView tvTitle;
    private ImageView ivLeft,ivRight;

    //包含的属性
    private String title;
    private int titleColor;
    private float titleSize;
    private Drawable drLeft,drRight;

    //布局参数
    private LayoutParams tvParam,leftParam,rightParam;

    /**
     * 回调接口设置监听事件的处理
     * 设置一个必须完成的接口做函方法参数,然后通过该接口对象调用其内部方法
     */
    public interface topbarClicklistener{
        public void leftClick();
        public void rightClick();
    }
    private topbarClicklistener clicklistener;

    public void setOnTopbarClick(topbarClicklistener clicklistener){
        this.clicklistener = clicklistener;
    }

    public MyTopbar(Context context, AttributeSet attrs) {
        super(context, attrs);

        //绑定属性文件
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTopbar);

        //绑定属性
        title = typedArray.getString(R.styleable.MyTopbar_mytitle);
        titleColor = typedArray.getColor(R.styleable.MyTopbar_mytitleColor, 0);
        titleSize = typedArray.getDimension(R.styleable.MyTopbar_mytitleSize, 0);
        drLeft = typedArray.getDrawable(R.styleable.MyTopbar_leftBg);
        drRight = typedArray.getDrawable(R.styleable.MyTopbar_rightBg);

        //新建控件
        tvTitle = new TextView(context);
        ivLeft = new ImageView(context);
        ivRight = new ImageView(context);

        /**
         * 及时回收,方便重用
         */
        typedArray.recycle();

        //设置属性控件
        tvTitle.setText(title);
        tvTitle.setTextColor(titleColor);
        tvTitle.setTextSize(titleSize);
        tvTitle.setGravity(Gravity.LEFT);

        ivLeft.setImageDrawable(drLeft);
        ivRight.setImageDrawable(drRight);


        //添加控件
        tvParam = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,1);
        addView(tvTitle, tvParam);   //添加控件并设置其布局

        leftParam = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT,0);
        addView(ivLeft,leftParam);

        rightParam = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT,0);
        addView(ivRight,rightParam);

        //点击事件
        ivLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                clicklistener.leftClick();
            }
        });

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

    }
}

在布局中使用

<com.plf.weknow.view.MyTopbar
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:id="@+id/mybar"
        android:background="@color/colorPrimaryDark"
        custom:mytitle="hehe"
        custom:mytitleColor="#f0f0f0"
        custom:mytitleSize="8sp"
        custom:leftBg="@drawable/search"
        custom:rightBg="@drawable/more">
    com.plf.weknow.view.MyTopbar>

你可能感兴趣的:(Android)