美文网首页
android 自定义 header

android 自定义 header

作者: 一个冬季 | 来源:发表于2020-03-10 17:43 被阅读0次
    自定义header.png
    可以修改哪些参数

    1、返回按钮图片替换、按钮宽高修改、按钮背景颜色修改
    2、整个headerLayout背景色的修改
    3、右边可以是文字、图片,可以修改文字大小,图片大小,图片背景色修改
    4、如果只设置了headerLayout的背景色,其它地方都没有设置颜色,那么其它地方都按headerLayout的背景色来

    更新

    2020-02-15
    1、增大返回按钮的宽高

    <declare-styleable name="HeaderViewLayout">
            <!--整个标题栏的颜色-->
            <attr name="headerBackColor" format="color"/>
    
            <!--左边按钮FrameLayout背景色-->
            <attr name="headerLeftFrameBackColor" format="color"/>
            <!--左边按钮ImageView-->
            <attr name="headerLeftImage" format="reference"/>
            <!--左边按钮ImageView背景色-->
            <attr name="headerLeftImageBackColor" format="color"/>
            <!--左边按钮ImageView宽-->
            <attr name="headerLeftImageWidth" format="dimension"/>
            <!--左边按钮ImageView高-->
            <attr name="headerLeftImageHeight" format="dimension"/>
    
            <!--中间文字-->
            <attr name="headerMiddleText" format="string"/>
            <!--中间文字大小-->
            <attr name="headerMiddleTextSize" format="integer"/>
            <!--中间文字颜色-->
            <attr name="headerMiddleTextColor" format="color"/>
    
            <!--右边文字-->
            <attr name="headerRightText" format="string"/>
            <!--右边文字大小-->
            <attr name="headerRightTextSize" format="integer"/>
            <!--右边文字颜色-->
            <attr name="headerRightTextColor" format="color"/>
    
            <!--右边按钮FrameLayout背景色-->
            <attr name="headerRightFrameBackColor" format="color"/>
            <!--右边按钮ImageView-->
            <attr name="headerRightImage" format="reference"/>
            <!--右边按钮ImageView背景色-->
            <attr name="headerRightImageBackColor" format="color"/>
            <!--右边按钮ImageView宽-->
            <attr name="headerRightImageWidth" format="dimension"/>
            <!--右边按钮ImageView高-->
            <attr name="headerRightImageHeight" format="dimension"/>
        </declare-styleable>
    
        <color name="app_theme_color_sysm">#FB4C7F</color>
    
        <item name="base_header_framelayout" type="id"/>
        <item name="base_header_framelayout_back" type="id"/>
        <item name="base_header_framelayout_right" type="id"/>
        <item name="iv_baseheader_right_iv" type="id"/>
        <item name="tv_baseheader_right_text" type="id"/>
        <item name="tv_base_header_title" type="id"/>
    
        /**
         * @date :2019/11/27 0027
         * @author : gaoxiaoxiong
         * @description:由浮点型转成整型时,原则是忽略小数点部分。
         **/
        public int getDimensionPixelOffset(Context context,int id){
            return context.getResources().getDimensionPixelOffset(id);
        }
    
    
    /**
     * @date: 创建时间:2020/3/5
     * @author: gaoxiaoxiong
     * @descripion:设置标题
     **/
    public class HeaderViewLayout extends FrameLayout {
        private Context mContext;
        private int headerBackColor;//整个View的背景色
    
        //左边
        private int flLeftLayoutBackGroundColor;
        private int ivLeftBackImageViewResource;
        private int ivLeftBackImageViewBackGroundColor;
        private int ivLeftBackImageWidth, ivLeftBackImageHeight;
    
        //中间文字
        private int middleTextSize;
        private String middleText;
        private int middleTextColor;
    
        //右边文字
        private int rightTextSize;
        private String rightText;
        private int rightTextColor;
    
        //右边图片
        private int flRightLayoutBackGroundColor;
        private int ivRightBackImageViewBackGroundColor;
        private int ivRightBackImageViewResource;
        private int ivRightBackImageWidth, ivRightBackImageHeight;
    
        public HeaderViewLayout(@NonNull Context context) {
            this(context, null);
        }
    
        public HeaderViewLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public HeaderViewLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            this.mContext = context;
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HeaderViewLayout);
            headerBackColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerBackColor, ContextCompat.getColor(mContext, R.color.app_theme_color_sysm));
    
            //左边
            flLeftLayoutBackGroundColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerLeftFrameBackColor, 0);
            ivLeftBackImageViewResource = typedArray.getResourceId(R.styleable.HeaderViewLayout_headerLeftImage, R.drawable.icon_back_white);
            ivLeftBackImageViewBackGroundColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerLeftImageBackColor, 0);
            ivLeftBackImageHeight = typedArray.getDimensionPixelOffset(R.styleable.HeaderViewLayout_headerLeftImageHeight, mContext.getResources().getDimensionPixelOffset(R.dimen.dp_25));
            ivLeftBackImageWidth = typedArray.getDimensionPixelOffset(R.styleable.HeaderViewLayout_headerLeftImageWidth, mContext.getResources().getDimensionPixelOffset(R.dimen.dp_25));
    
            //中间
            middleText = typedArray.getString(R.styleable.HeaderViewLayout_headerMiddleText);
            middleTextSize = typedArray.getInt(R.styleable.HeaderViewLayout_headerMiddleTextSize, 18);
            middleTextColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerMiddleTextColor, ContextCompat.getColor(mContext, R.color.white));
    
            //右边
            flRightLayoutBackGroundColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerRightFrameBackColor, 0);
    
            //右边文字
            rightTextSize = typedArray.getInt(R.styleable.HeaderViewLayout_headerRightTextSize,14);
            rightTextColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerRightTextColor, ContextCompat.getColor(mContext, R.color.black_333333));
            rightText = typedArray.getString(R.styleable.HeaderViewLayout_headerRightText);
    
            //右边图片
            ivRightBackImageViewBackGroundColor = typedArray.getColor(R.styleable.HeaderViewLayout_headerRightImageBackColor, 0);
            ivRightBackImageViewResource = typedArray.getResourceId(R.styleable.HeaderViewLayout_headerRightImage, 0);
            ivRightBackImageWidth = typedArray.getDimensionPixelOffset(R.styleable.HeaderViewLayout_headerRightImageWidth,mContext.getResources().getDimensionPixelOffset(R.dimen.dp_25));
            ivRightBackImageHeight = typedArray.getDimensionPixelOffset(R.styleable.HeaderViewLayout_headerRightImageHeight,mContext.getResources().getDimensionPixelOffset(R.dimen.dp_25));
    
            typedArray.recycle();
            init();
        }
    
        private void init() {
            //左边
            FrameLayout frameBackLayout = new FrameLayout(mContext);
            frameBackLayout.setId(R.id.base_header_framelayout_back);
            FrameLayout.LayoutParams flrameBackLayoutLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            flrameBackLayoutLayoutParams.gravity = Gravity.CENTER_VERTICAL;
            frameBackLayout.setPadding(DensityUtil.getInstance().getDimensionPixelOffset(mContext, R.dimen.dp_10), 0, DensityUtil.getInstance().getDimensionPixelOffset(mContext, R.dimen.dp_10), 0);
            int totalWidth = ivLeftBackImageWidth + DensityUtil.getInstance().getDimensionPixelOffset(mContext, R.dimen.dp_10) * 2 + DensityUtil.getInstance().getDimensionPixelOffset(mContext, R.dimen.dp_5);
            flrameBackLayoutLayoutParams.width = totalWidth;
            frameBackLayout.setLayoutParams(flrameBackLayoutLayoutParams);
            if (flLeftLayoutBackGroundColor != 0) {
                frameBackLayout.setBackgroundColor(flLeftLayoutBackGroundColor);
            }
    
    
            ImageView ivBackImageView = new ImageView(mContext);
            FrameLayout.LayoutParams ivBackImageLayoutParams = new FrameLayout.LayoutParams(ivLeftBackImageWidth, ivLeftBackImageHeight);
            ivBackImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            ivBackImageView.setLayoutParams(ivBackImageLayoutParams);
            ivBackImageLayoutParams.gravity = Gravity.CENTER;
            if (ivLeftBackImageViewBackGroundColor != 0) {
                ivBackImageView.setBackgroundColor(ivLeftBackImageViewBackGroundColor);
            }
    
            ivBackImageView.setImageResource(ivLeftBackImageViewResource);
            frameBackLayout.addView(ivBackImageView);
            this.addView(frameBackLayout);
    
            //中间文字
            TextView middleTextView = new TextView(mContext);
            middleTextView.setId(R.id.tv_base_header_title);
            FrameLayout.LayoutParams middleTextViewLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            middleTextViewLayoutParams.gravity = Gravity.CENTER;
            middleTextView.setTextSize(middleTextSize);
            middleTextView.setGravity(Gravity.CENTER);
            middleTextView.setTextColor(middleTextColor);
            middleTextView.setText(StringUtils.isEmptyValue(middleText));
            middleTextView.setLayoutParams(middleTextViewLayoutParams);
            this.addView(middleTextView);
    
            //右边
            FrameLayout frameRightLayout = new FrameLayout(mContext);
            frameRightLayout.setId(R.id.base_header_framelayout_right);
            FrameLayout.LayoutParams frameRightLayoutLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            frameRightLayoutLayoutParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
            frameRightLayout.setLayoutParams(frameRightLayoutLayoutParams);
            frameRightLayout.setPadding(DensityUtil.getInstance().getDimensionPixelOffset(mContext, R.dimen.dp_10), 0, DensityUtil.getInstance().getDimensionPixelOffset(mContext, R.dimen.dp_10), 0);
            if (flRightLayoutBackGroundColor != 0) {
                frameRightLayout.setBackgroundColor(flRightLayoutBackGroundColor);
            }
            if (!StringUtils.isEmpty(rightText) || ivRightBackImageViewResource != 0) {
                if (!StringUtils.isEmpty(rightText)) {//显示右边文字
                    TextView rightTextView = new TextView(mContext);
                    rightTextView.setId(R.id.tv_baseheader_right_text);
                    FrameLayout.LayoutParams rightTextViewLayoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
                    rightTextViewLayoutParams.gravity = Gravity.CENTER;
                    rightTextView.setTextSize(rightTextSize);
                    rightTextView.setGravity(Gravity.CENTER);
                    rightTextView.setText(StringUtils.isEmptyValue(rightText));
                    rightTextView.setTextColor(rightTextColor);
                    rightTextView.setLayoutParams(rightTextViewLayoutParams);
                    frameRightLayout.addView(rightTextView);
                } else {
                    ImageView ivRightImageView = new ImageView(mContext);
                    ivRightImageView.setId(R.id.iv_baseheader_right_iv);
                    FrameLayout.LayoutParams ivRightImageViewLayoutParams = new FrameLayout.LayoutParams(ivRightBackImageWidth, ivRightBackImageHeight);
                    ivRightImageViewLayoutParams.gravity = Gravity.CENTER;
                    ivRightImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    ivRightImageView.setLayoutParams(ivRightImageViewLayoutParams);
                    if (ivRightBackImageViewBackGroundColor != 0) {
                        ivRightImageView.setBackgroundColor(ivRightBackImageViewBackGroundColor);
                    }
                    ivRightImageView.setImageResource(ivRightBackImageViewResource);
                    frameRightLayout.addView(ivRightImageView);
                }
            }
            this.addView(frameRightLayout);
            this.setId(R.id.base_header_framelayout);
            this.setBackgroundColor(headerBackColor);
        }
    
     /**
         * @date 创建时间:2020/5/15 0015
         * @auther gaoxiaoxiong
         * @Descriptiion 添加右边的视图,会删除之前写好的视图
         **/
        public void setRightView(View view){
            FrameLayout.LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
            layoutParams.gravity = Gravity.CENTER;
            view.setLayoutParams(layoutParams);
            FrameLayout frameRightLayout = this.findViewById(R.id.base_header_framelayout_right);
            frameRightLayout.removeAllViews();
            frameRightLayout.addView(view);
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:android 自定义 header

          本文链接:https://www.haomeiwen.com/subject/mrlgdhtx.html