android 通用标题栏

通用标题栏

public class NormalTitleView extends RelativeLayout {

    private int textColor = 0xffffff;

    /**default TextView drawable-padding with dp unit*/
    private int drawablePadding = 3;
    /**min view width with dp unit*/
    private int minViewWidth = 35;
    /**the size of smaller text with sp unit*/
    private int defaultSmallTextSize = 16;
    /**the size of title text with sp unit*/
    private int defaultTitleSize = 20;

    /*left button, if this means back,two options:1.image button;2:a textView which has drawable and text*/
    /**back to last page--TextView*/
    private TextView mLeftBackTextTv;
    /**back to last page--ImageView*/
    private ImageView mLeftBackImageTv;

    /**title*/
    private TextView mTitleTv;

    /*right button, ImageView or TextView ,only show one of them*/
    /**right ImageView*/
    private ImageView mRightImageIv;
    /**right TextView*/
    private TextView mRightTextTv;

    /*right another button, ImageView or TextView ,only show one of them*/
    /**right another ImageView*/
    private ImageView mRightImageTwoIv;
    /**right another TextView*/
    private TextView mRightTextTwoTv;



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

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

    public NormalTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }


    /**
     * initview
     * @param context Context
     * @param attrs AttributeSet
     */
    private void initView (Context context, AttributeSet attrs) {
        TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.TitleAttr);

        /*init text and image widget*/
        initLeftTextView(context, typeArray);
        initLeftImageView(context, typeArray);
        initTitleView(context, typeArray);
        initRightTextView(context, typeArray);
        initRightImageView(context, typeArray);
        initRightAnotherTextView(context, typeArray);
        initRightAnotherImageView(context, typeArray);

        typeArray.recycle();

    }

    /**
     * init the left TextView
     * @param context Context
     * @param typeArray TypedArray
     */
    private void initLeftTextView (Context context, TypedArray typeArray) {
        int leftText = typeArray.getResourceId(R.styleable.TitleAttr_left_text, 0);
        CharSequence charSequence =  leftText > 0 ?
                typeArray.getResources().getText(leftText) : typeArray.getString(R.styleable.TitleAttr_left_text);

        LayoutParams params = initLayoutParams();
        /*init TextView*/
        mLeftBackTextTv = createTextView(context, R.id.tv_left_text, charSequence, params);
        setTextViewDrawable(typeArray, R.styleable.TitleAttr_left_text_drawable_left, R.styleable.TitleAttr_left_text_drawable_right, mLeftBackTextTv);

        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        float textSize = getDimensionPixelSize(typeArray, R.styleable.TitleAttr_small_text_size, defaultSmallTextSize);
        mLeftBackTextTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        mLeftBackTextTv.setTextColor(getTextColorFromAttr(typeArray));
        addView(mLeftBackTextTv);
    }

    /**
     * init the left ImageView
     * @param context Context
     * @param typeArray TypedArray
     */
    private void initLeftImageView (Context context, TypedArray typeArray) {
        int leftImgAttr = typeArray.getResourceId(R.styleable.TitleAttr_left_image, 0);

        if (leftImgAttr != 0) {
            LayoutParams params = initLayoutParams();
            mLeftBackImageTv = createImageView(context, R.id.iv_left_image, leftImgAttr, params);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            addView(mLeftBackImageTv);
        }

    }



    /**
     * init the title TextView
     * @param context Context
     * @param typeArray TypedArray
     */
    private void initTitleView (Context context, TypedArray typeArray) {
        int leftText = typeArray.getResourceId(R.styleable.TitleAttr_title_name, 0);
        CharSequence charSequence =  leftText > 0 ?
                typeArray.getResources().getText(leftText) : typeArray.getString(R.styleable.TitleAttr_title_name);

        LayoutParams params = initLayoutParams();
        /*init TextView*/
        mTitleTv = createTextView(context, R.id.tv_title_name, charSequence, params);
        setTextViewDrawable(typeArray, R.styleable.TitleAttr_title_drawable_left, R.styleable.TitleAttr_title_drawable_right, mTitleTv);


        float textSize = getDimensionPixelSize(typeArray, R.styleable.TitleAttr_title_text_size, defaultTitleSize);
        mTitleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        mTitleTv.setTextColor(getTextColorFromAttr(typeArray));

        int gravity = typeArray.getInt(R.styleable.TitleAttr_title_gravity, 0);
        if (gravity > 0) {
            if (gravity == Gravity.LEFT) {
                params.addRule(RelativeLayout.RIGHT_OF, mLeftBackImageTv == null ? mLeftBackTextTv.getId() : mLeftBackImageTv.getId());
            }
        } else { //default:center
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
        }

        addView(mTitleTv);
    }

    /**
     * init the right TextView
     * @param context Context
     * @param typeArray TypedArray
     */
    private void initRightTextView (Context context, TypedArray typeArray) {
        int leftText = typeArray.getResourceId(R.styleable.TitleAttr_right_text, 0);
        CharSequence charSequence =  leftText > 0 ?
                typeArray.getResources().getText(leftText) : typeArray.getString(R.styleable.TitleAttr_right_text);

        LayoutParams params = initLayoutParams();
        /*init TextView*/
        mRightTextTv = createTextView(context, R.id.tv_right_text, charSequence, params);
        setTextViewDrawable(typeArray, R.styleable.TitleAttr_right_text_drawable_left, R.styleable.TitleAttr_right_text_drawable_right, mRightTextTv);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        float textSize = getDimensionPixelSize(typeArray, R.styleable.TitleAttr_small_text_size, defaultSmallTextSize);
        mRightTextTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        mRightTextTv.setTextColor(getTextColorFromAttr(typeArray));

        addView(mRightTextTv);
    }

    /**
     * init the right another TextView
     * @param context Context
     * @param typeArray TypedArray
     */
    private void initRightAnotherTextView (Context context, TypedArray typeArray) {
        int leftText = typeArray.getResourceId(R.styleable.TitleAttr_right_text_two, 0);
        CharSequence charSequence =  leftText > 0 ?
                typeArray.getResources().getText(leftText) : typeArray.getString(R.styleable.TitleAttr_right_text_two);

        LayoutParams params = initLayoutParams();
        /*init TextView*/
        mRightTextTwoTv = createTextView(context, R.id.tv_right_text_two, charSequence, params);
        setTextViewDrawable(typeArray, R.styleable.TitleAttr_right_text_two_drawable_left,  R.styleable.TitleAttr_right_text_two_drawable_right, mRightTextTwoTv);
        if (mRightImageIv != null) {
            params.addRule(RelativeLayout.LEFT_OF, mRightImageIv.getId());
        } else if (mRightTextTv != null) {
            params.addRule(RelativeLayout.LEFT_OF, mRightTextTv.getId());
        }

        float textSize = getDimensionPixelSize(typeArray, R.styleable.TitleAttr_small_text_size, defaultSmallTextSize);
        mRightTextTwoTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        mRightTextTwoTv.setTextColor(getTextColorFromAttr(typeArray));

        addView(mRightTextTwoTv);
    }

    /**
     * init the right ImageView
     * @param context Context
     * @param typeArray TypedArray
     */
    private void initRightImageView (Context context, TypedArray typeArray) {
        int rightImgAttr = typeArray.getResourceId(R.styleable.TitleAttr_right_image, 0);

        if (rightImgAttr == 0) {
            return;
        }
        LayoutParams params = initLayoutParams();
        mRightImageIv = createImageView(context, R.id.iv_right_image, rightImgAttr, params);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        addView(mRightImageIv);

    }

    /**
     * init the right ImageView
     * @param context Context
     * @param typeArray TypedArray
     */
    private void initRightAnotherImageView (Context context, TypedArray typeArray) {
        int rightImgAttr = typeArray.getResourceId(R.styleable.TitleAttr_right_image_two, 0);

        if (rightImgAttr == 0) {
            return;
        }
        LayoutParams params = initLayoutParams();
        mRightImageTwoIv = createImageView(context, R.id.iv_right_image_two, rightImgAttr, params);

        if (mRightImageIv != null) {
            params.addRule(RelativeLayout.LEFT_OF, mRightImageIv.getId());
        } else if (mRightTextTv != null) {
            params.addRule(RelativeLayout.LEFT_OF, mRightTextTv.getId());
        }
        addView(mRightImageTwoIv);

    }


    /**
     * layout params of RelativeLayout
     * @return LayoutParams
     */
    private LayoutParams initLayoutParams () {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
    }


    /**
     * drawable of TextView
     * @param typeArray TypedArray
     * @param leftDrawableStyleable leftDrawableStyleable
     * @param rightDrawableStyleable rightDrawableStyleable
     * @param textView which TextView to set
     */
    private void setTextViewDrawable(TypedArray typeArray, int leftDrawableStyleable, int rightDrawableStyleable, TextView textView) {
        int leftDrawable = typeArray.getResourceId(leftDrawableStyleable, 0);
        int rightDrawable = typeArray.getResourceId(rightDrawableStyleable, 0);
        textView.setCompoundDrawablePadding((int)getPixelSizeByDp(drawablePadding));
        textView.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, 0, rightDrawable, 0);

    }

    /**
     * create TextView
     * @param context Context
     * @param id the id of TextView
     * @param charSequence text to show
     * @param params relative params
     * @return the TextView which is inited
     */
    @NonNull
    private TextView createTextView(Context context, int id, CharSequence charSequence, LayoutParams params) {
        TextView textView = new TextView(context);
        textView.setLayoutParams(params);
        textView.setGravity(Gravity.CENTER);
        textView.setId(id);
        textView.setMinWidth((int)getPixelSizeByDp(minViewWidth));
        textView.setText(charSequence);
        return textView;
    }

    /**
     * create ImageView
     * @param context Context
     * @param id the id of ImageView
     * @param drawable the resource of imageView
     * @param params relative params
     * @return ImageView
     */
    @NonNull
    private ImageView createImageView (Context context, int id, int drawable, LayoutParams params) {
        ImageView imageView = new ImageView(context);
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setId(id);
        imageView.setMinimumWidth((int)getPixelSizeByDp(minViewWidth));
        imageView.setImageResource(drawable);
        return imageView;
    }

    /**
     * get Pixel size by dp
     * @param dp dp
     * @return the value of px
     */
    private float getPixelSizeByDp(int dp) {
        Resources res = this.getResources();
        final float scale = res.getDisplayMetrics().density;
        return  dp * scale + 0.5f;
    }

    /**
     * get Pixel size by sp
     * @param sp sp
     * @return the value of px
     */
    private float getPiselSizeBySp (int sp) {
        Resources res = this.getResources();
        final float scale = res.getDisplayMetrics().scaledDensity;
        return sp * scale;
    }

    /**
     * get the dimension pixel size from typeArray which is defined in attr
     * @param typeArray TypedArray
     * @param stylable stylable
     * @param defaultSize defaultSize
     * @return the dimension pixel size
     */
    private float getDimensionPixelSize(TypedArray typeArray, int stylable, int defaultSize) {
        int sizeStyleable = typeArray.getResourceId(stylable, 0);
        return sizeStyleable > 0 ? typeArray.getResources().getDimensionPixelSize(sizeStyleable) : typeArray.getDimensionPixelSize(stylable, (int)getPiselSizeBySp(defaultSize));
    }


    /**
     * get textColor
     * @param typeArray TypedArray
     * @return textColor
     */
    private int getTextColorFromAttr (TypedArray typeArray) {
        int textColorStyleable = typeArray.getResourceId(R.styleable.TitleAttr_title_text_color, 0);
        if (textColorStyleable > 0) {
            return typeArray.getResources().getColor(textColorStyleable);
        } else {
            return typeArray.getColor(R.styleable.TitleAttr_title_text_color, textColor);
        }

    }

    /**
     * set TextView's drawable padding
     * @param drawablePadding drawablePadding
     */
    public void setTextViewDrawablePadding(int drawablePadding) {
        this.drawablePadding = drawablePadding;
    }

    public TextView getLeftBackTextTv() {
        return mLeftBackTextTv;
    }

    public ImageView getLeftBackImageTv() {
        return mLeftBackImageTv;
    }

    public TextView getTitleTv() {
        return mTitleTv;
    }

    public ImageView getRightImageIv() {
        return mRightImageIv;
    }

    public TextView getRightTextTv() {
        return mRightTextTv;
    }

    public ImageView getRightImageTwoIv() {
        return mRightImageTwoIv;
    }

    public TextView getRightTextTwoTv() {
        return mRightTextTwoTv;
    }

    public void setTitle(String title ){
        mTitleTv.setText(title);
    }
}

在attrs.xml中定义如下:

<!--&lt;!&ndash; 标题属性 &ndash;&gt;-->
    <declare-styleable name="TitleAttr">
        <attr name="title_name" format="reference|string"/>
        <attr name="title_text_color" format="reference|color"/>
        <attr name="title_drawable_right" format="reference"/>
        <attr name="title_drawable_left" format="reference"/>
        <attr name="title_text_size" format="reference|dimension"/>
        <attr name="left_text" format="reference|string"/>
        <attr name="left_image" format="reference"/>
        <attr name="small_text_size" format="reference|dimension"/>
        <attr name="title_gravity">
            <enum name="left" value="3"/>
            <enum name="center" value="17"/>
            <enum name="right" value="5"/>
        </attr>
        <attr name="right_image" format="reference"/>
        <attr name="right_text" format="reference|string"/>
        <attr name="right_image_two" format="reference"/>
        <attr name="right_text_two" format="reference|string"/>
        <attr name="title_height" format="dimension"/>
        <attr name="right_text_drawable_right" format="reference"/>
        <attr name="right_text_drawable_left" format="reference"/>
        <attr name="right_text_two_drawable_right" format="reference"/>
        <attr name="right_text_two_drawable_left" format="reference"/>
        <attr name="left_text_drawable_right" format="reference"/>
        <attr name="left_text_drawable_left" format="reference"/>
    </declare-styleable>

使用方法如下:

<com.util.NormalTitleView
        android:layout_width="match_parent"
        android:layout_height="44dp"
        title:left_image="@mipmap/xxx"
        title:right_image="@mipmap/xxx"
        title:small_text_size="15sp"
        title:title_name="xxx"
        title:title_text_color="#333333"
        title:title_text_size="19dp" />

activity中调用方法:

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

推荐阅读更多精彩内容