自定义通用的标题条目

转载请注明出处: http://blog.csdn.net/forwardyzk/article/details/43760891

在开发中在Activity,Fragment中,我们一般都会增加标题的展示,现在自定义一个控件,可以在每个界面都可以使用。下面先看一下效果图:

使用的方法是自定义属性,首先在res/values/attres.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TitleView">
        <!--左侧TextView属性-->
        <attr name="title_LeftText" format="string"></attr>
        <attr name="title_LeftColor" format="color|reference"></attr>
        <attr name="title_LeftBg" format="color|reference"></attr>
        <attr name="title_LeftSize" format="dimension"></attr>
        <attr name="title_LeftPadding" format="dimension"></attr>
        <attr name="title_LeftPaddingLeft" format="dimension"></attr>
        <attr name="title_LeftPaddingRight" format="dimension"></attr>
        <attr name="title_LeftPaddingBottom" format="dimension"></attr>
        <attr name="title_LeftPaddingTop" format="dimension"></attr>
        <attr name="title_LeftMargin" format="dimension"></attr>
        <attr name="title_LeftMarginBottom" format="dimension"></attr>
        <attr name="title_LeftMarginTop" format="dimension"></attr>
        <attr name="title_LeftMarginLeft" format="dimension"></attr>
        <attr name="title_LeftMarginRight" format="dimension"></attr>
        <attr name="title_LeftVisibility">
            <enum name="GONE" value="0"></enum>
            <enum name="INVISIBLE" value="1"></enum>
            <enum name="VISIBLE" value="2"></enum>
        </attr>

        <!--右侧TextView属性-->
        <attr name="title_RightText" format="string"></attr>
        <attr name="title_RightColor" format="color|reference"></attr>
        <attr name="title_RightBg" format="color|reference"></attr>
        <attr name="title_RightSize" format="dimension"></attr>
        <attr name="title_RightPadding" format="dimension"></attr>
        <attr name="title_RightPaddingLeft" format="dimension"></attr>
        <attr name="title_RightPaddingRight" format="dimension"></attr>
        <attr name="title_RightPaddingBottom" format="dimension"></attr>
        <attr name="title_RightPaddingTop" format="dimension"></attr>
        <attr name="title_RightMargin" format="dimension"></attr>
        <attr name="title_RightMarginBottom" format="dimension"></attr>
        <attr name="title_RightMarginTop" format="dimension"></attr>
        <attr name="title_RightMarginLeft" format="dimension"></attr>
        <attr name="title_RightMarginRight" format="dimension"></attr>
        <attr name="title_RightVisibility">
            <enum name="GONE" value="0"></enum>
            <enum name="INVISIBLE" value="1"></enum>
            <enum name="VISIBLE" value="2"></enum>
        </attr>
        <!--中间TextView属性-->
        <attr name="title_MiddleText" format="string"></attr>
        <attr name="title_MiddleColor" format="color|reference"></attr>
        <attr name="title_MiddleBg" format="color|reference"></attr>
        <attr name="title_MiddleSize" format="dimension"></attr>
        <attr name="title_MiddlePadding" format="dimension"></attr>
        <attr name="title_MiddlePaddingLeft" format="dimension"></attr>
        <attr name="title_MiddlePaddingRight" format="dimension"></attr>
        <attr name="title_MiddlePaddingBottom" format="dimension"></attr>
        <attr name="title_MiddlePaddingTop" format="dimension"></attr>
        <attr name="title_MiddleMargin" format="dimension"></attr>
        <attr name="title_MiddleMarginBottom" format="dimension"></attr>
        <attr name="title_MiddleMarginTop" format="dimension"></attr>
        <attr name="title_MiddleMarginLeft" format="dimension"></attr>
        <attr name="title_MiddleMarginRight" format="dimension"></attr>
        <attr name="title_MiddleVisibility">
            <enum name="GONE" value="0"></enum>
            <enum name="INVISIBLE" value="1"></enum>
            <enum name="VISIBLE" value="2"></enum>
        </attr>

    </declare-styleable>
</resources>

例如:<attr name="title_LeftColor" format="color|reference"></attr>可以是颜色色值,也可以是引用的背景资源文件

属性有左侧TextView,右侧TextView和中间TextView

设置文本的颜色,大小,文本内容

设置TextView的背景

设置TextView的Padding,PaddinfLeft,PaddingRight,PaddingTop,PaddingBottom

设置TextView的Margin,MarginLeft,MarginRight,MarginTop,MarginBottom

设置TextView是否显示

自定义TitleView继承RelativeLayout

public class TitleView extends RelativeLayout {
    private Context mContext;
    private float DEFAULT_TEXT_SIZE = 20;
    private int DEFAULT_TEXT_COLOR = Color.BLACK;
    private float DEFAULT_PADDING = 2;
    private float DEFAULT_MARGIN = 0;
    private int GONE = 0;
    private int INVISIBLE = 1;
    private int VISIBLE = 2;

    //左侧属性
    private String leftText;
    private float leftSize;
    private int leftColor;
    private Drawable leftBg;
    private float leftPadding;
    private float leftPaddingLeft;
    private float leftPaddingRight;
    private float leftPaddingTop;
    private float leftPaddingBottom;
    private float leftMargin;
    private float leftMarginLeft;
    private float leftMarginRight;
    private float leftMarginBottom;
    private float leftMarginTop;
    //右侧属性
    private String rightText;
    private float rightSize;
    private int rightColor;
    private Drawable rightBg;
    private float rightPadding;
    private float rightPaddingLeft;
    private float rightPaddingRight;
    private float rightPaddingTop;
    private float rightPaddingBottom;
    private float rightMargin;
    private float rightMarginLeft;
    private float rightMarginRight;
    private float rightMarginBottom;
    private float rightMarginTop;
    //中间属性
    private String middleText;
    private float middleSize;
    private int middleColor;
    private Drawable middleBg;
    private float middlePadding;
    private float middlePaddingLeft;
    private float middlePaddingRight;
    private float middlePaddingTop;
    private float middlePaddingBottom;
    private float middleMargin;
    private float middleMarginLeft;
    private float middleMarginRight;
    private float middleMarginBottom;
    private float middleMarginTop;
    private TextView leftTextView;
    private TextView rightTextView;
    private TextView middleTextView;

    private int leftVisibity;
    private int rightVisibity;
    private int middleVisibity;
    private OnClickTitleViewListener mOnClickTitleViewListener;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
        // 获取左侧TextView属性
        leftText = typedArray.getString(R.styleable.TitleView_title_LeftText);
        leftSize = typedArray.getDimension(R.styleable.TitleView_title_LeftSize, DEFAULT_TEXT_SIZE);
        leftColor = typedArray.getColor(R.styleable.TitleView_title_LeftColor, DEFAULT_TEXT_COLOR);
        leftBg = typedArray.getDrawable(R.styleable.TitleView_title_LeftBg);
        leftPadding = typedArray.getDimension(R.styleable.TitleView_title_LeftPadding, DEFAULT_PADDING);

        leftPaddingLeft = typedArray.getDimension(R.styleable.TitleView_title_LeftPaddingLeft, DEFAULT_PADDING);
        leftPaddingRight = typedArray.getDimension(R.styleable.TitleView_title_LeftPaddingRight, DEFAULT_PADDING);
        leftPaddingTop = typedArray.getDimension(R.styleable.TitleView_title_LeftPaddingTop, DEFAULT_PADDING);
        leftPaddingBottom = typedArray.getDimension(R.styleable.TitleView_title_LeftPaddingBottom, DEFAULT_PADDING);
        if (leftPadding != DEFAULT_PADDING) {
            leftPaddingLeft = leftPaddingRight = leftPaddingTop = leftPaddingBottom = leftPadding;
        }
        leftMargin = typedArray.getDimension(R.styleable.TitleView_title_LeftMargin, DEFAULT_MARGIN);
        leftMarginTop = typedArray.getDimension(R.styleable.TitleView_title_LeftMarginTop, DEFAULT_MARGIN);
        leftMarginBottom = typedArray.getDimension(R.styleable.TitleView_title_LeftMarginBottom, DEFAULT_MARGIN);
        leftMarginRight = typedArray.getDimension(R.styleable.TitleView_title_LeftMarginRight, DEFAULT_MARGIN);
        leftMarginLeft = typedArray.getDimension(R.styleable.TitleView_title_LeftMarginLeft, DEFAULT_MARGIN);
        if (leftMargin != DEFAULT_MARGIN) {
            leftMarginLeft = leftMarginRight = leftMarginTop = leftMarginBottom = leftMargin;
        }
        //获取右侧TextView属性
        rightText = typedArray.getString(R.styleable.TitleView_title_RightText);
        rightSize = typedArray.getDimension(R.styleable.TitleView_title_RightSize, DEFAULT_TEXT_SIZE);
        rightColor = typedArray.getColor(R.styleable.TitleView_title_RightColor, DEFAULT_TEXT_COLOR);
        rightBg = typedArray.getDrawable(R.styleable.TitleView_title_RightBg);
        rightPadding = typedArray.getDimension(R.styleable.TitleView_title_RightPadding, DEFAULT_PADDING);

        rightPaddingLeft = typedArray.getDimension(R.styleable.TitleView_title_RightPaddingLeft, DEFAULT_PADDING);
        rightPaddingRight = typedArray.getDimension(R.styleable.TitleView_title_RightPaddingRight, DEFAULT_PADDING);
        rightPaddingTop = typedArray.getDimension(R.styleable.TitleView_title_RightPaddingTop, DEFAULT_PADDING);
        rightPaddingBottom = typedArray.getDimension(R.styleable.TitleView_title_RightPaddingBottom, DEFAULT_PADDING);
        if (rightPadding != DEFAULT_PADDING) {
            rightPaddingLeft = rightPaddingRight = rightPaddingTop = rightPaddingBottom = rightPadding;
        }
        rightMargin = typedArray.getDimension(R.styleable.TitleView_title_RightMargin, DEFAULT_MARGIN);

        rightMarginTop = typedArray.getDimension(R.styleable.TitleView_title_RightMarginTop, DEFAULT_MARGIN);
        rightMarginBottom = typedArray.getDimension(R.styleable.TitleView_title_RightMarginBottom, DEFAULT_MARGIN);
        rightMarginRight = typedArray.getDimension(R.styleable.TitleView_title_RightMarginRight, DEFAULT_MARGIN);
        rightMarginLeft = typedArray.getDimension(R.styleable.TitleView_title_RightMarginLeft, DEFAULT_MARGIN);
        if (rightMargin != DEFAULT_MARGIN) {
            rightMarginLeft = rightMarginRight = rightMarginTop = rightMarginBottom = rightMargin;
        }
        //中间TextView属性
        middleText = typedArray.getString(R.styleable.TitleView_title_MiddleText);
        middleSize = typedArray.getDimension(R.styleable.TitleView_title_MiddleSize, DEFAULT_TEXT_SIZE);
        middleColor = typedArray.getColor(R.styleable.TitleView_title_MiddleColor, DEFAULT_TEXT_COLOR);
        middleBg = typedArray.getDrawable(R.styleable.TitleView_title_MiddleBg);
        middlePadding = typedArray.getDimension(R.styleable.TitleView_title_MiddlePadding, DEFAULT_PADDING);

        middlePaddingLeft = typedArray.getDimension(R.styleable.TitleView_title_MiddlePaddingLeft, DEFAULT_PADDING);
        middlePaddingRight = typedArray.getDimension(R.styleable.TitleView_title_MiddlePaddingRight, DEFAULT_PADDING);
        middlePaddingTop = typedArray.getDimension(R.styleable.TitleView_title_MiddlePaddingTop, DEFAULT_PADDING);
        middlePaddingBottom = typedArray.getDimension(R.styleable.TitleView_title_MiddlePaddingBottom, DEFAULT_PADDING);
        if (middlePadding != DEFAULT_PADDING) {
            middlePaddingLeft = middlePaddingRight = middlePaddingTop = middlePaddingBottom = middlePadding;
        }
        middleMargin = typedArray.getDimension(R.styleable.TitleView_title_MiddleMargin, DEFAULT_MARGIN);

        middleMarginTop = typedArray.getDimension(R.styleable.TitleView_title_MiddleMarginTop, DEFAULT_MARGIN);
        middleMarginBottom = typedArray.getDimension(R.styleable.TitleView_title_MiddleMarginBottom, DEFAULT_MARGIN);
        middleMarginRight = typedArray.getDimension(R.styleable.TitleView_title_MiddleMarginRight, DEFAULT_MARGIN);
        middleMarginLeft = typedArray.getDimension(R.styleable.TitleView_title_MiddleMarginLeft, DEFAULT_MARGIN);
        if (middleMargin != DEFAULT_MARGIN) {
            middleMarginLeft = middleMarginRight = middleMarginTop = middleMarginBottom = middleMargin;
        }
        //是否显示
        middleVisibity = typedArray.getInt(R.styleable.TitleView_title_MiddleVisibility, VISIBLE);
        leftVisibity = typedArray.getInt(R.styleable.TitleView_title_LeftVisibility, VISIBLE);
        rightVisibity = typedArray.getInt(R.styleable.TitleView_title_RightVisibility, VISIBLE);
        initTextView();
        setAttributeSet();
        setTitleListener();
    }

    /**
     * init leftTextView,rightTextView,middleTextView
     */
    private void initTextView() {
        leftTextView = new TextView(mContext);
        rightTextView = new TextView(mContext);
        middleTextView = new TextView(mContext);
        //left
        LayoutParams leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
        leftParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
        leftParams.setMargins((int) leftMargin, (int) leftMargin, (int) leftMargin, (int) leftMargin);
        leftParams.setMargins((int) leftMarginLeft, (int) leftMarginTop, (int) leftMarginRight, (int) leftMarginBottom);
        leftTextView.setLayoutParams(leftParams);
        leftTextView.setPadding((int) leftPadding, (int) leftPadding, (int) leftPadding, (int) leftPadding);
        leftTextView.setPadding((int) leftPaddingLeft, (int) leftPaddingTop, (int) leftPaddingRight, (int) leftPaddingBottom);

        //right
        LayoutParams rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
        rightParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
        rightParams.setMargins((int) rightMargin, (int) rightMargin, (int) rightMargin, (int) rightMargin);
        rightParams.setMargins((int) rightMarginLeft, (int) rightMarginTop, (int) rightMarginRight, (int) rightMarginBottom);
        rightTextView.setLayoutParams(rightParams);
        rightTextView.setPadding((int) rightPadding, (int) rightPadding, (int) rightPadding, (int) rightPadding);
        rightTextView.setPadding((int) rightPaddingLeft, (int) rightPaddingTop, (int) rightPaddingRight, (int) rightPaddingBottom);
        //middle
        LayoutParams middleParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        middleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
        middleParams.setMargins((int) middleMargin, (int) middleMargin, (int) middleMargin, (int) middleMargin);
        middleParams.setMargins((int) middleMarginLeft, (int) middleMarginTop, (int) middleMarginRight, (int) middleMarginBottom);
        middleTextView.setLayoutParams(middleParams);
        middleTextView.setPadding((int) middlePadding, (int) middlePadding, (int) middlePadding, (int) middlePadding);
        middleTextView.setPadding((int) middlePaddingLeft, (int) middlePaddingTop, (int) middlePaddingRight, (int) middlePaddingBottom);

        addView(leftTextView);
        addView(rightTextView);
        addView(middleTextView);
    }

    /**
     * set leftTextView,rightTextView,middleTextView AttributeSet
     */
    private void setAttributeSet() {
        //left
        leftTextView.setText(leftText);
        leftTextView.setTextSize(leftSize);
        leftTextView.setTextColor(leftColor);
        leftTextView.setBackground(leftBg);
        //right
        rightTextView.setText(rightText);
        rightTextView.setTextSize(rightSize);
        rightTextView.setTextColor(rightColor);
        rightTextView.setBackground(rightBg);
        //middle
        middleTextView.setText(middleText);
        middleTextView.setTextSize(middleSize);
        middleTextView.setTextColor(middleColor);
        middleTextView.setBackground(middleBg);
        setTextViewVisibility(middleTextView, middleVisibity);
        setTextViewVisibility(leftTextView, leftVisibity);
        setTextViewVisibility(rightTextView, rightVisibity);
    }

    /**
     * set leftTextView,rightTextView,middleTextView Visibility
     */
    private void setTextViewVisibility(TextView tv, int visibility) {
        if (visibility == GONE || visibility == View.GONE) {
            tv.setVisibility(View.GONE);
        } else if (visibility == INVISIBLE || visibility == View.INVISIBLE) {
            tv.setVisibility(View.INVISIBLE);
        } else if (visibility == VISIBLE || visibility == View.VISIBLE) {
            tv.setVisibility(View.VISIBLE);
        }
    }

    /**
     * set leftTextView,rightTextView,middleTextView OnClickListener
     */
    private void setTitleListener() {

        leftTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickTitleViewListener != null) {
                    mOnClickTitleViewListener.onClickLeft();
                }
            }
        });
        rightTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickTitleViewListener != null) {
                    mOnClickTitleViewListener.onClickRight();
                }
            }
        });
        middleTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickTitleViewListener != null) {
                    mOnClickTitleViewListener.onClickMiddle();
                }
            }
        });

    }

    public interface OnClickTitleViewListener {
        public void onClickLeft();

        public void onClickRight();

        public void onClickMiddle();
    }

    public void setOnClickTitleViewListener(OnClickTitleViewListener onClickTitleViewListener) {
        this.mOnClickTitleViewListener = onClickTitleViewListener;
    }

    /**
     * set leftTextView visibity
     *
     * @param visibity
     */
    public void setLeftVisibity(int visibity) {
        setTextViewVisibility(leftTextView, visibity);

    }

    /**
     * set rightTextView visibity
     *
     * @param visibity
     */
    public void setRightVisibity(int visibity) {
        setTextViewVisibility(rightTextView, visibity);

    }

    /**
     * set middleTextView visibity
     *
     * @param visibity
     */
    public void setMiddleVisibity(int visibity) {
        setTextViewVisibility(middleTextView, visibity);

    }

    /**
     * set left textview margins
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setLeftMargins(int left, int top, int right, int bottom) {
        LayoutParams layoutParams = (LayoutParams) leftTextView.getLayoutParams();
        layoutParams.setMargins(left, top, right, bottom);
        leftTextView.setLayoutParams(layoutParams);
    }

    /**
     * set right textview margins
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setRightMargins(int left, int top, int right, int bottom) {
        LayoutParams layoutParams = (LayoutParams) rightTextView.getLayoutParams();
        layoutParams.setMargins(left, top, right, bottom);
        rightTextView.setLayoutParams(layoutParams);
    }

    /**
     * set middle textview margins
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setMiddleMargins(int left, int top, int right, int bottom) {
        LayoutParams layoutParams = (LayoutParams) middleTextView.getLayoutParams();
        layoutParams.setMargins(left, top, right, bottom);
        middleTextView.setLayoutParams(layoutParams);
    }

    /**
     * set left textview padding
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setLeftPadding(int left, int top, int right, int bottom) {
        leftTextView.setPadding(left, top, right, bottom);
    }

    /**
     * set right textview padding
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setRightPadding(int left, int top, int right, int bottom) {
        rightTextView.setPadding(left, top, right, bottom);
    }

    /**
     * set middle textview padding
     *
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setMiddlePadding(int left, int top, int right, int bottom) {
        middleTextView.setPadding(left, top, right, bottom);
    }

    /**
     * set left TextView show text
     *
     * @param leftText
     */
    public void setLeftText(String leftText) {
        leftTextView.setText(leftText);
    }

    /**
     * set left TextView textSize
     *
     * @param leftTextSize
     */
    public void setLeftTextSize(float leftTextSize) {
        leftTextView.setTextSize(leftTextSize);
    }

    /**
     * set left TextView textColor
     *
     * @param leftTextColor
     */
    public void setLeftTextColor(int leftTextColor) {
        leftTextView.setTextColor(leftTextColor);
    }

    /**
     * set left TextView background
     *
     * @param leftBg
     */
    public void setLeftBg(Drawable leftBg) {
        leftTextView.setBackground(leftBg);
    }

    /**
     * set left TextView BackgroundResource
     *
     * @param leftBgResourceId
     */
    public void setLeftBgResource(int leftBgResourceId) {
        leftTextView.setBackgroundResource(leftBgResourceId);
    }

    /**
     * set left TextView BackgroundColor
     *
     * @param leftBgColor
     */
    public void setLeftBgColor(int leftBgColor) {
        leftTextView.setBackgroundColor(leftBgColor);
    }

    /**
     * set right TextView show text
     *
     * @param rightText
     */
    public void setRightText(String rightText) {
        rightTextView.setText(rightText);
    }

    /**
     * set right TextView textSize
     *
     * @param rightTextSize
     */
    public void setRightTextSize(float rightTextSize) {
        rightTextView.setTextSize(rightTextSize);
    }

    /**
     * set right TextView textColor
     *
     * @param rightTextColor
     */
    public void setRightTextColor(int rightTextColor) {
        rightTextView.setTextColor(rightTextColor);
    }

    /**
     * set right TextView background
     *
     * @param rightBg
     */
    public void setRightBg(Drawable rightBg) {
        rightTextView.setBackground(rightBg);
    }

    /**
     * set right TextView BackgroundResource
     *
     * @param rightBgResourceId
     */
    public void setRightBgResource(int rightBgResourceId) {
        rightTextView.setBackgroundResource(rightBgResourceId);
    }

    /**
     * set right TextView BackgroundColor
     *
     * @param rightBgColor
     */
    public void setRightBgColor(int rightBgColor) {
        rightTextView.setBackgroundColor(rightBgColor);
    }

    /**
     * set middle TextView show text
     *
     * @param middleText
     */
    public void setMiddleText(String middleText) {
        middleTextView.setText(middleText);
    }

    /**
     * set middle TextView textSize
     *
     * @param middleTextSize
     */
    public void setMiddleTextSize(float middleTextSize) {
        middleTextView.setTextSize(middleTextSize);
    }

    /**
     * set middle TextView textColor
     *
     * @param middleTextColor
     */
    public void setMiddleTextColor(int middleTextColor) {
        middleTextView.setTextColor(middleTextColor);
    }

    /**
     * set middle TextView background
     *
     * @param middleBg
     */
    public void setMiddleBg(Drawable middleBg) {
        middleTextView.setBackground(middleBg);
    }

    /**
     * set middle TextView BackgroundResource
     *
     * @param middleBgResourceId
     */
    public void setMiddleBgResource(int middleBgResourceId) {
        middleTextView.setBackgroundResource(middleBgResourceId);
    }

    /**
     * set middle TextView BackgroundColor
     *
     * @param middleBgColor
     */
    public void setMiddleBgColor(int middleBgColor) {
        middleTextView.setBackgroundColor(middleBgColor);
    }
}

使用代码创建TextView:

leftTextView = new TextView(mContext);

设置位置和大小:

LayoutParams leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

leftTextView.setLayoutParams(leftParams);

获取属性的类型数组:

ypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleView);

获取设置的字符串文本内容:

typedArray.getString(R.styleable.TitleView_title_LeftText);

获取文本的大小和间距(Padding,Margin):

typedArray.getDimension(R.styleable.TitleView_title_LeftSize, DEFAULT_TEXT_SIZE);

获取设置的颜色,例如文本的颜色:

typedArray.getColor(R.styleable.TitleView_title_LeftColor, DEFAULT_TEXT_COLOR);

获取设置的背景:

typedArray.getDrawable(R.styleable.TitleView_title_LeftBg);

获取是否显示:

typedArray.getInt(R.styleable.TitleView_title_MiddleVisibility, VISIBLE);

使用代码设置自定义的属性对应的值

就以左侧TextView为例

设置是否显示:

public void setLeftVisibity(int visibity)

设置控件之间间距:

public void setLeftMargins(int left, int top, int right, int bottom)

设置内间距:

public void setLeftPadding(int left, int top, int right, int bottom)

设置文本内容:

public void setLeftText(String leftText)

设置文本大小:

public void setLeftTextSize(float leftTextSize)

设置文本颜色:

public void setLeftTextColor(int leftTextColor)

设置背景图片:

public void setLeftBg(Drawable leftBg)

设置背景资源:

public void setLeftBgResource(int leftBgResourceId)

设置背景颜色:

public void setLeftBgColor(int leftBgColor)

注意点

1.如果在调用处设置了Padding,同时也设置了PaddingLeft,PaddingRight,PaddingTop,PaddingBottom,那么以Padding为准,如果只设置一个方向,那么就不要设置Padding属性

2.如果在调用处设置了Margin,同时也设置了MarginLeft,MarginRight,MarginTop,MarginBottom,那么以Margin为准,如果只设置一个方向,那么就不要设置Padding属性

从这个就可以看出:

if (middleMargin != DEFAULT_MARGIN) {
            middleMarginLeft = middleMarginRight = middleMarginTop = middleMarginBottom = middleMargin;
        }
 if (middlePadding != DEFAULT_PADDING) {
            middlePaddingLeft = middlePaddingRight = middlePaddingTop = middlePaddingBottom = middlePadding;
        }

TitleView中三个TextView设置点击事件:

setOnClickTitleViewListener(OnClickTitleViewListener onClickTitleViewListener)

public interface OnClickTitleViewListener {
        public void onClickLeft();

        public void onClickRight();

        public void onClickMiddle();
    }
public void setOnClickTitleViewListener(OnClickTitleViewListener onClickTitleViewListener) {
        this.mOnClickTitleViewListener = onClickTitleViewListener;
    }
private void setTitleListener() {

        leftTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickTitleViewListener != null) {
                    mOnClickTitleViewListener.onClickLeft();
                }
            }
        });
        rightTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickTitleViewListener != null) {
                    mOnClickTitleViewListener.onClickRight();
                }
            }
        });
        middleTextView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickTitleViewListener != null) {
                    mOnClickTitleViewListener.onClickMiddle();
                }
            }
        });

    }

在调用处,如果设置点击事件,那么就要实现这个三个方法。onClickLeft(),onClickRight(),onClickMiddle()

使用步骤:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:title="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <com.example.administrator.titleviewdemo.view.TitleView
                android:layout_marginBottom="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                title:title_LeftText="left"
                title:title_MiddleText="title"
                title:title_RightText="right"
                title:title_MiddleSize="20sp"
                title:title_LeftSize="15sp"
                title:title_LeftColor="@android:color/holo_red_dark"
                title:title_RightColor="@android:color/holo_red_dark"
                title:title_MiddleColor="@android:color/holo_green_dark"
                android:background="@android:color/holo_blue_dark">

            </com.example.administrator.titleviewdemo.view.TitleView>

            <com.example.administrator.titleviewdemo.view.TitleView
                android:layout_marginBottom="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                title:title_LeftText="left"
                title:title_MiddleText="title"
                title:title_MiddleSize="25sp"
                title:title_LeftSize="20sp"
                title:title_RightSize="10sp"
                title:title_RightText="right"
                title:title_LeftVisibility="GONE"
                title:title_RightPaddingRight="20dp"
                title:title_LeftColor="@android:color/holo_red_dark"
                title:title_RightColor="@android:color/holo_red_dark"
                title:title_MiddleColor="@android:color/holo_green_dark"
                android:background="@android:color/holo_blue_dark">

            </com.example.administrator.titleviewdemo.view.TitleView>

            <com.example.administrator.titleviewdemo.view.TitleView
                android:layout_marginBottom="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                title:title_LeftText="left"
                title:title_MiddleText="title"
                title:title_MiddleSize="30sp"
                title:title_LeftSize="25sp"
                title:title_RightSize="20sp"
                title:title_RightText="right"
                title:title_RightVisibility="GONE"
                title:title_LeftPaddingLeft="20dp"
                title:title_LeftColor="@android:color/holo_red_dark"
                title:title_RightColor="@android:color/holo_red_dark"
                title:title_MiddleColor="@android:color/holo_green_dark"
                android:background="@android:color/holo_blue_dark">

            </com.example.administrator.titleviewdemo.view.TitleView>

            <com.example.administrator.titleviewdemo.view.TitleView
                android:layout_marginBottom="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                title:title_LeftText="left"
                title:title_MiddleText="title"
                title:title_MiddleSize="35sp"
                title:title_RightText="right"
                title:title_MiddleVisibility="GONE"
                title:title_LeftMargin="10dp"
                title:title_RightMarginRight="10dp"
                title:title_LeftColor="@android:color/holo_red_dark"
                title:title_RightColor="@android:color/holo_red_dark"
                title:title_MiddleColor="@android:color/holo_green_dark"
                android:background="@android:color/holo_blue_dark">

            </com.example.administrator.titleviewdemo.view.TitleView>

            <com.example.administrator.titleviewdemo.view.TitleView
                android:id="@+id/title_view"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:layout_marginBottom="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                title:title_MiddleText="title"
                title:title_MiddleSize="40sp"
                title:title_MiddleColor="@android:color/black"
                title:title_LeftBg="@drawable/left"
                title:title_RightBg="@drawable/right"
                android:background="@android:color/holo_blue_dark">

            </com.example.administrator.titleviewdemo.view.TitleView>

            <com.example.administrator.titleviewdemo.view.TitleView
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:layout_marginBottom="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                title:title_MiddleText="title"
                title:title_MiddleSize="45sp"
                title:title_MiddleColor="@android:color/holo_green_dark"
                title:title_LeftBg="@drawable/left"
                title:title_RightBg="@drawable/right"
                title:title_LeftVisibility="GONE"
                android:background="@android:color/darker_gray">

            </com.example.administrator.titleviewdemo.view.TitleView>

            <com.example.administrator.titleviewdemo.view.TitleView
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:layout_marginBottom="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                title:title_MiddleText="title"
                title:title_MiddleSize="50sp"
                title:title_MiddleColor="@android:color/holo_green_dark"
                title:title_LeftBg="@drawable/left"
                title:title_RightBg="@drawable/right"
                title:title_RightVisibility="GONE"
                android:background="@android:color/holo_blue_dark">

            </com.example.administrator.titleviewdemo.view.TitleView>

            <com.example.administrator.titleviewdemo.view.TitleView
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:layout_marginBottom="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                title:title_MiddleText="title"
                title:title_MiddleSize="55sp"
                title:title_MiddleColor="@android:color/holo_green_dark"
                title:title_LeftBg="@drawable/left"
                title:title_RightBg="@drawable/right"
                title:title_MiddleVisibility="GONE"
                android:background="@android:color/darker_gray">

            </com.example.administrator.titleviewdemo.view.TitleView>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

在跟节点中要写入xmlns:title="http://schemas.android.com/apk/res-auto",如果是在eclipse,res-auto要换成res/完整的应用包名

title是名字随便命名,

<com.example.administrator.titleviewdemo.view.TitleView//完整的包名+类名

title:title_LeftText="left"

title:title_MiddleText="title"

title:title_RightText="right"

title:title_MiddleSize="20sp" >

</com.example.administrator.titleviewdemo.view.TitleView>

title:前缀  title_LeftText属性

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TitleView titleView = (TitleView) findViewById(R.id.title_view);
        //设置点击事件
        titleView.setOnClickTitleViewListener(new TitleView.OnClickTitleViewListener() {
            @Override
            public void onClickLeft() {
                Toast.makeText(getApplicationContext(), "click-left", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onClickRight() {
                Toast.makeText(getApplicationContext(), "click-right", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onClickMiddle() {
                Toast.makeText(getApplicationContext(), "click-middle", Toast.LENGTH_SHORT).show();
            }
        });
        //使用代码设置中间TextView的间距
        titleView.setMiddleMargins(0, 10, 0, 10);

    }

}

源码下载:
http://download.csdn.net/detail/forwardyzk/8442549

时间: 2024-10-24 20:54:06

自定义通用的标题条目的相关文章

自定义导航栏标题按钮

自定义标题栏按钮 @implementation SNTitleButton - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //图片居中 self.imageView.contentMode = UIViewContentModeCenter; //字体居右 self.titleLabel.textAlignment = NSTextAlignmentRight; //字体

IOS 自定义导航栏标题和返回按钮标题

IOS中自定义导航栏标题: UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(160, 0, 120, 50)]; titleText.backgroundColor = [UIColor clearColor]; titleText.textColor=[UIColor whiteColor]; [titleText setFont:[UIFont systemFontOfSize:17.0]]; [titleTex

WPF报表自定义通用可筛选列头-WPF特工队内部资料

由于项目需要制作一个可通用的报表多行标题,且可实现各种类型的内容显示,包括文本.输入框.下拉框.多选框等(自定的显示内容可自行扩展),并支持参数绑定转换,效果如下: 源码结构 ColumnItem类:对列的宽度,对象方式,显示类型,绑定名称,converter资源进行设置 /// <summary> /// 描述 <see cref="ColumnItem"/> 动态列项目,用于在后台构建多列标题绑定项 /// </summary> [Seriali

使用微信JSSDK自定义微信分享标题、描述、和图标

最近做一个项目的时候用到微信的分享 ,实现定义分享标题,图片,了解到微信在发布JSSDK后,把包括自定义分享在内的众多网页服务接口进行了统一.如果要想自定义分享自己的网页信息给好友或朋友圈,就最好使用这个统一的网页服务接口. 微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微信分享.扫一扫.卡券.支付等微信特有的能力,为微信用户提供更优质的网页体验. 步

2.Android 自定义通用的Item布局

转载:http://www.jianshu.com/p/e7ba4884dcdd BaseItemLayout 简介 在工作中经常会遇到下面的一些布局,如图标红处: 05.png 07.png 08.png 因此自定义了一个通用的Item布局,只需几行代码就可以根据需要添加任意个item. 使用 添加Gradle依赖 在 build.gradle 的 dependencies 添加 dependencies { ... compile 'com.maiml:baseitemlayoutlibra

Android 自定义通用的loadingview

介绍 好久没有写博客啦,最近在接近新年了,年前的工作都要收尾,所以特别忙,周末抽空写了个通用的加载view,写篇博客分享出来. 功能 1.显示加载视图,加载失败的时候显示加载失败视图,数据为空时显示数据为空视图,支持为失败视图设置点击事件重新加载数据. 2.支持个性化设置,自定义设置 加载.失败.空数据视图. 先放一张效果图压压惊 实现 实现思路其实就是一个FrameLayout里添加三个布局做处理显示隐藏,自定义视图其实就是替换里面的view ,代码比较简单,如果直接看过我的自定义view系列

Android之自定义(上方标题随ViewPager手势慢慢滑动)

最近很蛋疼,项目要模仿网易新闻的样式去做.上次把仿网易新闻客户端的下拉刷新写出来了,这次是ViewPager的滑动,同时ViewPager的上面标题下划线跟随者移动,本来通过ViewPager的OnPagerChangeListener的监听事件就可以完成,但是做出来之后,因为需要一直的刷新,所以很卡,一气之下,呵呵,自己完全的画了.整个点击事件,滑动事件都自己处理了. 效果图如下: 下标的长宽是随之改变的. 使用方式: 我的布局文件: <LinearLayout xmlns:android=&qu

制作通用模块标题

简单模块标题一般有文字和图片两种方式,文字除了标题还可以加更多链接,利用common.less可以把样式共用,每开发一次模板,只要把这标题做一次就可以了,非常方便. <group title="模块标题"> <section title="标题设置" folded="false"> <param description="" formType="range" label=&qu

ios7以上自定义导航栏标题的字体大小及颜色的方法

自定义导航栏的字体和颜色,只需要自定义一个lable,然后将lable添加到导航栏的titleview中就可以了 代码如下 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];//这个frame是初设的,没关系,后面还会重新设置其size. [label setNumberOfLines:0]; UIFont *font = [UIFont fontWithName:@"Arial" size:17]