为你的APP自定义一个统一的标题栏

通常的标题是分为几种?

输入图片说明

左边图标,左边文字, 中间文字 (修饰) , 右边文字, 右边图标

** ContextTitle .java **

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianjs.tianjinsuop2p.R;
import com.tianjs.tianjinsuop2p.utils.DensityUtil;

/**
 * 统一一个标题栏
 * Created by xiaolei on 2017/3/13.
 */

public class ContextTitle extends FrameLayout
{
    private int leftTextVisible = View.VISIBLE;
    private CharSequence leftText = "";
    private float leftTextSize = 14;
    private int leftTextColor = 0xff000000;

    private int leftImage = R.mipmap.ic_launcher;
    private int leftImageVisible = View.VISIBLE;

    private CharSequence titleText = "";
    private float titleTextSize = 14;
    private int titleTextColor = 0xff000000;
    private int titleVisible = View.VISIBLE;
    private int titleRightImg = -1;

    private int rightImage = R.mipmap.ic_launcher;
    private int rightImageVisible = View.VISIBLE;

    private int rightTextVisible = View.VISIBLE;
    private CharSequence rightText = "";
    private float rightTextSize = 14;
    private int rightTextColor = 0xff000000;

    private ImageView leftImageview;//左边图片
    private TextView leftTextview;//左边文字
    private TextView titleTextview;//标题文字
    private TextView rightTextview;//右边文字
    private ImageView rightImageview;//右边图片

    public ContextTitle(Context context)
    {
        this(context, null);
    }

    public ContextTitle(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public ContextTitle(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.ContextTitle);

        leftTextVisible = array.getBoolean(R.styleable.ContextTitle_leftTextVisible, false) ? VISIBLE : GONE;
        leftText = array.getText(R.styleable.ContextTitle_leftText);
        leftTextSize = array.getInt(R.styleable.ContextTitle_leftTextSize, 14);
        leftTextColor = array.getColor(R.styleable.ContextTitle_leftTextColor, 0xff000000);

        leftImageVisible = array.getBoolean(R.styleable.ContextTitle_leftImgVisible, false) ? VISIBLE : GONE;
        leftImage = array.getResourceId(R.styleable.ContextTitle_leftImg, R.mipmap.ic_launcher);

        titleText = array.getText(R.styleable.ContextTitle_titleText);
        titleTextSize = array.getInt(R.styleable.ContextTitle_titleTextSize, 14);
        titleTextColor = array.getColor(R.styleable.ContextTitle_titleTextColor, 0xff000000);
        titleVisible = array.getBoolean(R.styleable.ContextTitle_titleVisible, true) ? VISIBLE : GONE;
        titleRightImg = array.getResourceId(R.styleable.ContextTitle_titleRightImg, -1);

        rightImageVisible = array.getBoolean(R.styleable.ContextTitle_rightImgVisible, false) ? VISIBLE : GONE;
        rightImage = array.getResourceId(R.styleable.ContextTitle_rightImg, R.mipmap.ic_launcher);

        rightTextVisible = array.getBoolean(R.styleable.ContextTitle_rightTextVisible, false) ? VISIBLE : GONE;
        rightText = array.getText(R.styleable.ContextTitle_rightText);
        rightTextSize = array.getInt(R.styleable.ContextTitle_rightTextSize, 14);
        rightTextColor = array.getColor(R.styleable.ContextTitle_rightTextColor, 0xff000000);

        array.recycle();
        InitUI(context);
    }

    public void InitUI(Context context)
    {

        LinearLayout linearLayout = new LinearLayout(context);

        LayoutParams layoutparams1 = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(layoutparams1);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setGravity(Gravity.CENTER_VERTICAL);


        leftImageview = new ImageView(context);
        LinearLayout.LayoutParams leftImageparams = new LinearLayout.LayoutParams(DensityUtil.dip2px(context, 40), LayoutParams.MATCH_PARENT);
        leftImageparams.setMargins(DensityUtil.dip2px(context, 8), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0));
        leftImageview.setLayoutParams(leftImageparams);
        leftImageview.setImageResource(leftImage);
        leftImageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        leftImageview.setVisibility(leftImageVisible);
        linearLayout.addView(leftImageview);

        leftTextview = new TextView(context);
        LinearLayout.LayoutParams leftTextparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        leftTextparams.setMargins(DensityUtil.dip2px(context, 8), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, -8), DensityUtil.dip2px(context, 0));
        leftTextview.setLayoutParams(leftTextparams);
        leftTextview.setText(leftText);
        leftTextview.setGravity(Gravity.CENTER);
        leftTextview.setTextColor(leftTextColor);
        leftTextview.setTextSize(TypedValue.COMPLEX_UNIT_SP, leftTextSize);
        leftTextview.setVisibility(leftTextVisible);
        linearLayout.addView(leftTextview);

        titleTextview = new TextView(context);
        LayoutParams titleTextparams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
        titleTextview.setLayoutParams(titleTextparams);
        titleTextview.setText(titleText);
        titleTextview.setGravity(Gravity.CENTER);
        titleTextview.setTextSize(TypedValue.COMPLEX_UNIT_SP, titleTextSize);
        titleTextview.setVisibility(titleVisible);
        titleTextview.setTextColor(titleTextColor);
        if (titleRightImg > -1)
        {
            Drawable rightDrawable = context.getResources().getDrawable(titleRightImg);
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            titleTextview.setCompoundDrawables(null, null,rightDrawable, null);
            titleTextview.setCompoundDrawablePadding(DensityUtil.dip2px(context,5));
        }
        this.addView(titleTextview);

        LinearLayout.LayoutParams lineparams = new LinearLayout.LayoutParams(DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 1), 1);
        View lineView = new View(context);
        lineView.setLayoutParams(lineparams);
        linearLayout.addView(lineView);

        rightTextview = new TextView(context);
        LinearLayout.LayoutParams rightTextParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        rightTextParams.setMargins(DensityUtil.dip2px(context, -8), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 8), DensityUtil.dip2px(context, 0));
        rightTextview.setLayoutParams(rightTextParams);
        rightTextview.setText(rightText);
        rightTextview.setGravity(Gravity.CENTER);
        rightTextview.setTextColor(rightTextColor);
        rightTextview.setVisibility(rightTextVisible);
        rightTextview.setTextSize(TypedValue.COMPLEX_UNIT_SP, rightTextSize);
        linearLayout.addView(rightTextview);

        rightImageview = new ImageView(context);
        LinearLayout.LayoutParams rightImageparams = new LinearLayout.LayoutParams(DensityUtil.dip2px(context, 40), LayoutParams.MATCH_PARENT);
        rightImageparams.setMargins(DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 8), DensityUtil.dip2px(context, 0));
        rightImageview.setLayoutParams(rightImageparams);
        rightImageview.setImageResource(rightImage);
        rightImageview.setVisibility(rightImageVisible);
        rightImageview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        linearLayout.addView(rightImageview);

        this.addView(linearLayout);
    }

    public void setLeftTextVisible(int leftTextVisible)
    {
        this.leftTextVisible = leftTextVisible;
    }

    public void setLeftText(CharSequence leftText)
    {
        this.leftText = leftText;
    }

    /**
     * 默认单位SP
     *
     * @param leftTextSize
     */
    public void setLeftTextSize(float leftTextSize)
    {
        this.leftTextSize = leftTextSize;
    }

    public void setLeftTextColor(int leftTextColor)
    {
        this.leftTextColor = leftTextColor;
    }

    public void setLeftImage(int leftImage)
    {
        this.leftImage = leftImage;
    }

    public void setLeftImageVisible(int leftImageVisible)
    {
        this.leftImageVisible = leftImageVisible;
    }

    public void setTitleText(CharSequence titleText)
    {
        this.titleText = titleText;
        titleTextview.setText(titleText);
    }

    /**
     * 默认单位SP
     *
     * @param titleTextSize
     */
    public void setTitleTextSize(float titleTextSize)
    {
        this.titleTextSize = titleTextSize;
    }

    public void setTitleTextColor(int titleTextColor)
    {
        this.titleTextColor = titleTextColor;
    }

    public void setTitleVisible(int titleVisible)
    {
        this.titleVisible = titleVisible;
    }

    public void setRightImage(int rightImage)
    {
        this.rightImage = rightImage;
    }

    public void setRightImageVisible(int rightImageVisible)
    {
        this.rightImageVisible = rightImageVisible;
    }

    public void setRightTextVisible(int rightTextVisible)
    {
        this.rightTextVisible = rightTextVisible;
    }

    public void setRightText(CharSequence rightText)
    {
        this.rightText = rightText;
    }

    /**
     * 默认单位SP
     *
     * @param rightTextSize
     */
    public void setRightTextSize(float rightTextSize)
    {
        this.rightTextSize = rightTextSize;
    }

    public void setRightTextColor(int rightTextColor)
    {
        this.rightTextColor = rightTextColor;
    }


    public void setOnLeftImageClick(OnClickListener listener)
    {
        leftImageview.setOnClickListener(listener);
    }

    public void setOnLeftTextClick(OnClickListener listener)
    {
        leftTextview.setOnClickListener(listener);
    }

    public void setOnTitleClick(OnClickListener listener)
    {
        titleTextview.setOnClickListener(listener);
    }

    public void setOnRightTextClick(OnClickListener listener)
    {
        rightTextview.setOnClickListener(listener);
    }

    public void setOnRightImageClick(OnClickListener listener)
    {
        rightImageview.setOnClickListener(listener);
    }

    public void setTitleRightImg(@DrawableRes int titleRightImg)
    {
        this.titleRightImg = titleRightImg;
        if (titleRightImg > -1)
        {
            Drawable rightDrawable = getContext().getResources().getDrawable(titleRightImg);
            rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
            titleTextview.setCompoundDrawables(null, null,rightDrawable, null);
            titleTextview.setCompoundDrawablePadding(DensityUtil.dip2px(getContext(),5));
        }
    }

    /**
     * 获取标题文字
     * @return
     */
    public String getTitleText()
    {
        return titleTextview.getText().toString();
    }
    
    public void setLeftTextTag(Object tag)
    {
        leftTextview.setTag(tag);
    }
    public <T> T getLeftTextTag()
    {
        return (T)leftTextview.getTag();
    }
    
    public void setLeftImgTag(Object tag)
    {
        leftImageview.setTag(tag);
    }
    public <T> T getLeftImgTag()
    {
        return (T)leftImageview.getTag();
    }
    
    public void setTitleTextTag(Object tag)
    {
        titleTextview.setTag(tag);
    }
    public <T> T getTitleTextTag()
    {
        return (T)titleTextview.getTag();
    }

    public void setRightTextTag(Object tag)
    {
        rightTextview.setTag(tag);
    }
    public <T> T getRightTextTag()
    {
        return (T)rightTextview.getTag();
    }

    public void setRightImgTag(Object tag)
    {
        rightImageview.setTag(tag);
    }
    public <T> T getRightImgTag()
    {
        return (T)rightImageview.getTag();
    }
}

** styleable.xml **

<declare-styleable name="ContextTitle">
        <attr name="leftTextVisible" format="boolean"/>
        <attr name="leftText" format="string"/>
        <attr name="leftTextSize" format="integer"/>
        <attr name="leftTextColor" format="color"/>

        <attr name="leftImg" format="reference"/>
        <attr name="leftImgVisible" format="boolean"/>

        <attr name="titleText" format="string"/>
        <attr name="titleTextSize" format="integer"/>
        <attr name="titleTextColor" format="color"/>
        <attr name="titleVisible" format="boolean"/>
        <attr name="titleRightImg" format="reference"/>
        
        <attr name="rightImg" format="reference"/>
        <attr name="rightImgVisible" format="boolean"/>

        <attr name="rightTextVisible" format="boolean"/>
        <attr name="rightText" format="string"/>
        <attr name="rightTextSize" format="integer"/>
        <attr name="rightTextColor" format="color"/>
    </declare-styleable>

使用?

<com.tianjs.tianjinsuop2p.widgets.ContextTitle
        android:id="@+id/app_title"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/white"
        app:leftImg="@drawable/icon_back_black"
        app:leftImgVisible="true"
        app:rightText="充值记录"
        app:rightTextColor="@color/c_666666"
        app:rightTextSize="16"
        app:rightTextVisible="true"
        app:titleText="充值"
        app:titleTextColor="@color/main_text_color"
        app:titleTextSize="20"
        app:titleVisible="true"/>

上图?


输入图片说明

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏肖蕾的博客

使用Kotlin为你的APP自定义一个统一的标题栏

1135
来自专栏向治洪

android上拉下拉加载更多数据

最近项目中用到了ListView的下拉刷新的功能,总结了一下前辈们的代码,单独抽取出来写了一个demo作为示例。 效果图 下拉刷新: ? 加载更多: ? Cu...

2636
来自专栏技术小黑屋

Android处理图像数据全记录

Android中处理图像是一件很常见的事情,这里记录备忘一些亲身使用过的处理图片数据的方法。

561
来自专栏向治洪

android自定义密码键盘

先上图 ? 先看一下键盘的布局文件control_digitpasswordkeypad.xml <?xml version="1.0" encoding="u...

3685
来自专栏移动开发

一个可以在界面顶部展现的自定义 View

这个源码十分简单,没啥可说的.只有一点提一下,看到有些控件达到背景色的效果,是动态填加一个带背景色父布局实现的.

653
来自专栏Android干货

安卓开发_慕课网_Fragment实现Tab(App主界面)

34410
来自专栏Android干货

浅谈PopupWindow弹出菜单

28010
来自专栏Android知识点总结

D7-测试Android事件处理机制和生命函数调用

923
来自专栏james大数据架构

Activity间中使用Intent传值

主页面用来输入一个值传入第二个页面显示,关闭第二个页面返回一个值 主页布局: <RelativeLayout xmlns:android="http://sch...

17410
来自专栏Hongten

android开发_Intent高级学习(保存Activity状态)

http://www.cnblogs.com/hongten/gallery/image/112159.html

802

扫码关注云+社区