| | |
| | | */ |
| | | package com.dayu.pipirrapp.view; |
| | | |
| | | import android.content.Context; |
| | | import android.content.res.TypedArray; |
| | | import android.util.AttributeSet; |
| | | import android.util.Log; |
| | | import android.app.Activity; |
| | | import android.graphics.Color; |
| | | import android.text.TextUtils; |
| | | import android.view.View; |
| | | import android.widget.ImageButton; |
| | | import android.widget.ImageView; |
| | | import android.widget.LinearLayout; |
| | | import android.widget.RelativeLayout; |
| | | import android.widget.TextView; |
| | | |
| | | import com.dayu.pipirrapp.R; |
| | |
| | | * @author shisl |
| | | * @time 2015-4-29 |
| | | */ |
| | | public class TitleBar extends LinearLayout { |
| | | public class TitleBar { |
| | | /** |
| | | * 左侧/右侧图标和中间标题 |
| | | */ |
| | | private View titleView; |
| | | |
| | | private static final String TAG = "CNavigationBar"; |
| | | public static final int LEFT = 0; |
| | | public static final int CENTER = 1; |
| | | public static final int RIGHT = 2; |
| | | public static final int IMAGE = 3; |
| | | public static final int TEXT = 4; |
| | | public static final int ClickType_LEFT_TEXT = 1; |
| | | public static final int ClickType_LEFT_IMAGE = 2; |
| | | public static final int ClickType_CENTER_TEXT = 3; |
| | | public static final int ClickType_CENTER_IMAGE = 4; |
| | | public static final int ClickType_RIGHT_TEXT = 5; |
| | | public static final int ClickType_RIGHT_IMAGE = 6; |
| | | /** |
| | | * 左侧/右侧图标和中间标题 |
| | | */ |
| | | private RelativeLayout rl_title_bar; |
| | | |
| | | private TextView tvLeft; |
| | | private ImageView ivLeft; |
| | | private LinearLayout llLeft; |
| | | /** |
| | | * 跟布局 |
| | | */ |
| | | private LinearLayout ll_title_bar; |
| | | |
| | | private ImageButton ibCenter; |
| | | private TextView btnCenter; |
| | | /** |
| | | * 左侧图标 |
| | | */ |
| | | private ImageView iv_left_icon; |
| | | |
| | | private TextView tvRight; |
| | | private ImageView ivRight; |
| | | private LinearLayout llRight; |
| | | /** |
| | | * 右侧图标 |
| | | */ |
| | | private ImageView iv_rightIco; |
| | | |
| | | /** |
| | | * 中间标题 |
| | | */ |
| | | private TextView tv_title_middle; |
| | | |
| | | public TitleBar(Context context, AttributeSet attrs, int defStyle) { |
| | | super(context, attrs); |
| | | initView(context); |
| | | /** |
| | | * 右侧标题 |
| | | */ |
| | | private TextView tv_title_right; |
| | | |
| | | /** |
| | | * 构造方法:用于获取对象 |
| | | */ |
| | | public TitleBar(Activity context) { |
| | | titleView = context.findViewById(R.id.rl_title_bar); |
| | | rl_title_bar = (RelativeLayout) titleView.findViewById(R.id.rl_title_bar); |
| | | ll_title_bar = (LinearLayout) context.findViewById(R.id.ll_title_bar); |
| | | tv_title_middle = (TextView) titleView.findViewById(R.id.tv_title_middle); |
| | | tv_title_right = (TextView) titleView.findViewById(R.id.tv_title_right); |
| | | iv_left_icon = (ImageView) titleView.findViewById(R.id.iv_left_icon); |
| | | iv_rightIco = (ImageView) titleView.findViewById(R.id.iv_rightIco); |
| | | } |
| | | |
| | | /** |
| | | * 初始化我们自定义的组合控件 |
| | | * 用于设置标题栏文字 |
| | | * |
| | | * @param titleText 传入要设置的标题 |
| | | * @return |
| | | */ |
| | | private void initView(Context context) { |
| | | // 转化布局文件————>View对象,这个view对象直接挂载在自己(组合控件)身上 |
| | | View.inflate(context, R.layout.custom_titlebar, this); // this代表挂载到自己身上 |
| | | tvLeft = (TextView) this.findViewById(R.id.tv_titlebar_left); |
| | | btnCenter = (TextView) this.findViewById(R.id.btn_titlebar_center); |
| | | tvRight = (TextView) this.findViewById(R.id.tv_titlebar_right); |
| | | ivLeft = (ImageView) this.findViewById(R.id.iv_titlebar_left); |
| | | ibCenter = (ImageButton) this.findViewById(R.id.ib_titlebar_center); |
| | | ivRight = (ImageView) this.findViewById(R.id.iv_titlebar_right); |
| | | llLeft = (LinearLayout) this.findViewById(R.id.ll_left); |
| | | llRight = (LinearLayout) this.findViewById(R.id.ll_right); |
| | | } |
| | | |
| | | public TitleBar(Context context, AttributeSet attrs) { |
| | | super(context, attrs); |
| | | initView(context); |
| | | try { |
| | | /** |
| | | * 跟values/attrs.xml里面定义的属性绑定 |
| | | */ |
| | | // 从attrs.xml获取自定义的控件属性 |
| | | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleBar); |
| | | // //从attrs.xml获取自定义的value属性(控件属性名称+“_” + 自定义的属性名称) |
| | | String leftText = a.getString(R.styleable.TitleBar_leftText); |
| | | int leftImage = a.getResourceId(R.styleable.TitleBar_leftImage, 0); |
| | | String centerText = a.getString(R.styleable.TitleBar_centerText); |
| | | int centerImage = a.getResourceId(R.styleable.TitleBar_centerImage, 0); |
| | | String rightText = a.getString(R.styleable.TitleBar_rightText); |
| | | int rightImage = a.getResourceId(R.styleable.TitleBar_rightImage, 0); |
| | | a.recycle(); |
| | | setLeftText(leftText); |
| | | setRightText(rightText); |
| | | setCenterText(centerText); |
| | | setLeftImage(leftImage); |
| | | setRightImage(rightImage); |
| | | setCenterImage(centerImage); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | public TitleBar setTitleText(String titleText) { |
| | | if (!TextUtils.isEmpty(titleText)) { |
| | | tv_title_middle.setText(titleText); |
| | | } |
| | | return this; |
| | | } |
| | | |
| | | @Deprecated |
| | | public void setOnItemclickListner(int type, int aligh, OnClickListener listner) { |
| | | if (listner == null) { |
| | | return; |
| | | /** |
| | | * 设置标题栏文字颜色 |
| | | * |
| | | * @return |
| | | */ |
| | | public TitleBar setTitleTextColor() { |
| | | tv_title_middle.setTextColor(Color.WHITE); |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 设置标题栏右边的文字 |
| | | * |
| | | * @return |
| | | */ |
| | | public TitleBar setTitleRight(String rightTitle) { |
| | | if (!TextUtils.isEmpty(rightTitle)) { |
| | | tv_title_right.setVisibility(View.VISIBLE); |
| | | iv_rightIco.setVisibility(View.GONE); |
| | | tv_title_right.setTextColor(Color.WHITE); |
| | | tv_title_right.setText(rightTitle); |
| | | } |
| | | if (type == TEXT) { |
| | | switch (aligh) { |
| | | case LEFT: |
| | | setOnItemclickListner(ClickType_LEFT_TEXT, listner); |
| | | return this; |
| | | } |
| | | |
| | | break; |
| | | case CENTER: |
| | | setOnItemclickListner(ClickType_CENTER_TEXT, listner); |
| | | |
| | | break; |
| | | case RIGHT: |
| | | setOnItemclickListner(ClickType_RIGHT_TEXT, listner); |
| | | /** |
| | | * 用于设置标题栏左边要显示的图片 |
| | | * |
| | | * @param resId 标题栏左边的图标的id,一般为返回图标 |
| | | * @return |
| | | */ |
| | | public TitleBar setLeftIco(int resId) { |
| | | iv_left_icon.setVisibility(resId > 0 ? View.VISIBLE : View.GONE); |
| | | iv_left_icon.setImageResource(resId); |
| | | return this; |
| | | } |
| | | |
| | | break; |
| | | /** |
| | | * 用于设置标题栏左边要显示的图片 |
| | | * |
| | | * @return |
| | | */ |
| | | public TitleBar setLeftIco() { |
| | | iv_left_icon.setVisibility(View.VISIBLE); |
| | | iv_left_icon.setImageResource(R.drawable.vector_arrows_left_white); |
| | | return this; |
| | | } |
| | | |
| | | default: |
| | | break; |
| | | } |
| | | } else if (type == IMAGE) { |
| | | switch (aligh) { |
| | | case LEFT: |
| | | setOnItemclickListner(ClickType_LEFT_IMAGE, listner); |
| | | |
| | | break; |
| | | case CENTER: |
| | | setOnItemclickListner(ClickType_CENTER_IMAGE, listner); |
| | | /** |
| | | * 用于设置标题栏右边要显示的图片 |
| | | * |
| | | * @param resId 标题栏右边的图标id |
| | | * @return |
| | | */ |
| | | public TitleBar setRightIco(int resId) { |
| | | iv_rightIco.setVisibility(resId > 0 ? View.VISIBLE : View.GONE); |
| | | iv_rightIco.setImageResource(resId); |
| | | return this; |
| | | } |
| | | |
| | | break; |
| | | case RIGHT: |
| | | setOnItemclickListner(ClickType_RIGHT_IMAGE, listner); |
| | | break; |
| | | /** |
| | | * 用户设置 标题栏右侧的图标的背景drawable |
| | | * |
| | | * @param resId drawable的id |
| | | * @return |
| | | */ |
| | | public TitleBar setRightIconBgDr(int resId) { |
| | | iv_rightIco.setVisibility(resId > 0 ? View.VISIBLE : View.GONE); |
| | | // iv_rightIco.setImageResource(R.drawable.ic_back_selector); |
| | | return this; |
| | | } |
| | | |
| | | default: |
| | | break; |
| | | } |
| | | |
| | | /** |
| | | * 用户设置 标题栏右侧的图标的背景drawable |
| | | * |
| | | * @return |
| | | */ |
| | | public TitleBar setRightIconBgDr() { |
| | | iv_rightIco.setVisibility(View.GONE); |
| | | // iv_rightIco.setImageResource(R.drawable.ic_back_selector); |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | | * 用于设置标题栏左边图片的单击事件 |
| | | * |
| | | * @param listener 传入的事件对象 |
| | | * @return |
| | | */ |
| | | public TitleBar setLeftIcoListening(View.OnClickListener listener) { |
| | | if (iv_left_icon.getVisibility() == View.VISIBLE) { |
| | | iv_left_icon.setOnClickListener(listener); |
| | | } |
| | | |
| | | return this; |
| | | } |
| | | |
| | | public void setOnItemclickListner(int clickType, OnClickListener listner) { |
| | | try { |
| | | switch (clickType) { |
| | | case ClickType_LEFT_TEXT: |
| | | if (tvLeft.getVisibility() == View.VISIBLE) { |
| | | llLeft.setOnClickListener(listner); |
| | | } |
| | | break; |
| | | case ClickType_LEFT_IMAGE: |
| | | if (ivLeft.getVisibility() == View.VISIBLE) { |
| | | llLeft.setOnClickListener(listner); |
| | | } |
| | | /** |
| | | * 用于设置标题栏右边图片的单击事件 |
| | | * |
| | | * @param listener 传入的事件对象 |
| | | * @return |
| | | */ |
| | | public TitleBar setRightIcoListening(View.OnClickListener listener) { |
| | | |
| | | break; |
| | | case ClickType_CENTER_TEXT: |
| | | if (btnCenter.getVisibility() == View.VISIBLE) { |
| | | btnCenter.setOnClickListener(listner); |
| | | } |
| | | |
| | | break; |
| | | case ClickType_CENTER_IMAGE: |
| | | if (ibCenter.getVisibility() == View.VISIBLE) { |
| | | ibCenter.setOnClickListener(listner); |
| | | } |
| | | |
| | | break; |
| | | case ClickType_RIGHT_TEXT: |
| | | if (tvRight.getVisibility() == View.VISIBLE) { |
| | | llRight.setOnClickListener(listner); |
| | | } else { |
| | | llRight.setOnClickListener(null); |
| | | } |
| | | |
| | | break; |
| | | case ClickType_RIGHT_IMAGE: |
| | | if (ivRight.getVisibility() == View.VISIBLE) { |
| | | llRight.setOnClickListener(listner); |
| | | } |
| | | break; |
| | | |
| | | default: |
| | | break; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | if (iv_rightIco.getVisibility() == View.VISIBLE) { |
| | | iv_rightIco.setOnClickListener(listener); |
| | | } |
| | | |
| | | return this; |
| | | } |
| | | |
| | | public void setLeftImage(int leftImage) { |
| | | if (leftImage == 0) { |
| | | ivLeft.setVisibility(View.GONE); |
| | | return; |
| | | } |
| | | ivLeft.setVisibility(View.VISIBLE); |
| | | ivLeft.setImageResource(leftImage); |
| | | |
| | | } |
| | | |
| | | public void setRightImage(int rightImage) { |
| | | if (rightImage == 0) { |
| | | ivRight.setVisibility(View.GONE); |
| | | return; |
| | | } |
| | | ivRight.setVisibility(View.VISIBLE); |
| | | ivRight.setImageResource(rightImage); |
| | | |
| | | } |
| | | |
| | | private void setCenterImage(int leftImage) { |
| | | if (leftImage == 0) { |
| | | return; |
| | | } |
| | | ivLeft.setBackgroundResource(leftImage); |
| | | |
| | | } |
| | | |
| | | public void setLeftText(String leftText) { |
| | | if (leftText == null) { |
| | | return; |
| | | } |
| | | tvLeft.setVisibility(View.VISIBLE); |
| | | Log.i(TAG, "leftText--->" + leftText); |
| | | tvLeft.setText(leftText); |
| | | } |
| | | |
| | | public void setRightText(String rightText) { |
| | | if (rightText == null) { |
| | | return; |
| | | } |
| | | tvRight.setVisibility(View.VISIBLE); |
| | | llRight.setVisibility(View.VISIBLE); |
| | | tvRight.setText(rightText); |
| | | } |
| | | |
| | | public void setCenterText(String centerText) { |
| | | if (centerText == null) { |
| | | return; |
| | | } |
| | | btnCenter.setVisibility(View.VISIBLE); |
| | | Log.i(TAG, "centerText--->" + centerText); |
| | | btnCenter.setText(centerText); |
| | | } |
| | | |
| | | public void setRightStatus(boolean isable) { |
| | | tvRight.setEnabled(isable); |
| | | ivRight.setEnabled(isable); |
| | | llRight.setEnabled(isable); |
| | | |
| | | } |
| | | |
| | | public void setRightIMGVisibility(int Visibility) { |
| | | ivRight.setVisibility(Visibility); |
| | | } |
| | | |
| | | public TextView getTitleTextView() { |
| | | return btnCenter; |
| | | } |
| | | |
| | | public LinearLayout getLlRight() { |
| | | return llRight; |
| | | } |
| | | |
| | | } |