为你的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"/>

上图?


输入图片说明
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,332评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,508评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,812评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,607评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,728评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,919评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,071评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,802评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,256评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,576评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,712评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,389评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,032评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,026评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,473评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,606评论 2 350

推荐阅读更多精彩内容

  • 这篇文章只是对《为你的APP自定义一个统一的标题栏》这篇文章的Kotlin重写
    xiaolei123阅读 642评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,834评论 25 707
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,598评论 18 399
  • 这篇是关于篮球的,首先我是不会这的。因为别人也这么说,我便一直一直以初学者自居。其实一年的时间还是可以学到些东西的...
    高大杰阅读 152评论 0 0
  • ------顾城 我多么希望,有一个门口 早晨,阳光照在草上 我...
    深深暮雪辕门下阅读 148评论 0 0