转载请注明出处: 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