Android 打造一个通用的TitleBar
16lz
2021-01-23
每个App里都有标题栏模块,于是就封装一下组件 马上上代码:
public class TitleBar extends RelativeLayout { private static final String TAG = "TitleBar"; private ImageButton mBackButton; private ImageButton mShareButton; private TextView mTitle; private OnBackClickListener mOnBackClickListener; private OnShareClickListener mOnShareClickListener; private onRuleClickListner mRuleClickListner; private View mBottomLine; private boolean mEnableShare; private int mTitleColorResId; private int mBackIconResId; private int mShareIconResId; private int mTitleTextResId; private boolean mShowBottomLine; private int mRuleResId; private ImageButton mRuleButton; private boolean mEnableRule; private static int TITLECOLOR = R.color.common_text_color; private int mTitleColor = TITLECOLOR; private float mTitleTextSize; private boolean mTitleBold; public interface OnBackClickListener { void onBackClick(); } public interface OnShareClickListener { void onShareClick(); } public interface onRuleClickListner { void onRuleClick(); } public TitleBar(Context context) { super(context); initViews(context); } public TitleBar(Context context, AttributeSet attrs) { super(context, attrs); handleAttributes(context, attrs); initViews(context); } public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); handleAttributes(context, attrs); initViews(context); } private void handleAttributes(Context context, AttributeSet attrs) { try { TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TitleBar); mTitleBold = styledAttrs.getBoolean(R.styleable.TitleBar_title_text_bold, true); mTitleColor = styledAttrs.getResourceId(R.styleable.TitleBar_title_color, TITLECOLOR); mTitleTextSize = styledAttrs.getDimension(R.styleable.TitleBar_title_size, DimenUtil.dp2Px(18)); mEnableShare = styledAttrs.getBoolean(R.styleable.TitleBar_enable_share, false); mTitleColorResId = styledAttrs.getResourceId(R.styleable.TitleBar_title_color, R.color.common_text_color); mBackIconResId = styledAttrs.getResourceId(R.styleable.TitleBar_back_icon, R.drawable.ic_back); mShareIconResId = styledAttrs.getResourceId(R.styleable.TitleBar_share_icon, R.drawable.ic_share); mRuleResId = styledAttrs.getResourceId(R.styleable.TitleBar_integral_rule, R.drawable.ic_invalid_name); mTitleTextResId = styledAttrs.getResourceId(R.styleable.TitleBar_title_text, R.string.app_name); mEnableRule = styledAttrs.getBoolean(R.styleable.TitleBar_enable_rule, false); mShowBottomLine = styledAttrs.getBoolean(R.styleable.TitleBar_show_bottom_line, true); styledAttrs.recycle(); } catch (Exception e) { LogUtil.e(TAG, e.getMessage(), e); } } private void initViews(Context context) { LayoutInflater.from(context).inflate(R.layout.title_bar_layout, this); mBackButton = findViewById(R.id.title_back); mBackButton.setImageResource(mBackIconResId); mShareButton = findViewById(R.id.title_share); mRuleButton = findViewById(R.id.title_rule); mRuleButton.setImageResource(mRuleResId); mShareButton.setImageResource(mShareIconResId); if (mEnableShare) { mShareButton.setVisibility(VISIBLE); } if (mEnableRule) { mRuleButton.setVisibility(VISIBLE); } mTitle = findViewById(R.id.title_tv); mTitle.setText(mTitleTextResId); mTitle.setTextColor(context.getResources().getColor(mTitleColorResId)); mBottomLine = findViewById(R.id.bottom_line); if (!mShowBottomLine) { mBottomLine.setVisibility(GONE); } mBackButton.setOnClickListener(mOnClickListener); mTitle.setOnClickListener(mOnClickListener); mShareButton.setOnClickListener(mOnClickListener); mRuleButton.setOnClickListener(mOnClickListener); } public void setTitleColor(@ColorInt int titleColor) { mTitle.setTextColor(titleColor); } public void setBackIconResId(int backIconResId) { mBackIconResId = backIconResId; mBackButton.setImageResource(backIconResId); } public void setShareIconResId(int shareIconResId) { mShareIconResId = shareIconResId; mShareButton.setImageResource(shareIconResId); } public void setOnBackClickListener(OnBackClickListener listener) { mOnBackClickListener = listener; } public void setOnShareClickListener(OnShareClickListener listener) { mOnShareClickListener = listener; } public void setOnRuleClickListener(onRuleClickListner listener) { mRuleClickListner = listener; } private OnClickListener mOnClickListener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.title_back: { if (mOnBackClickListener != null) { mOnBackClickListener.onBackClick(); } break; } case R.id.title_tv: { break; } case R.id.title_share: { if (mOnShareClickListener != null) { mOnShareClickListener.onShareClick(); } break; } case R.id.title_rule: { if (mRuleClickListner != null) { mRuleClickListner.onRuleClick(); } } default: { break; } } return; } }; public void setTitle(String title) { mTitle.setText(title); } public void setTitleVisibility(int visibility) { mTitle.setVisibility(visibility); }}
布局文件 这里我们都将图片隐藏掉
attrs文件
好啦 现在代码是写完了,那么我们怎么用呢? 听我慢慢讲解,比如我们的状态栏里有分享 如下图
那么我们直接
把我们这个属性为true就可以了。
更多相关文章
- framework之 ContextImpl文件解析
- android 删除SD卡或者手机的缓存图片和目录
- Android外部存储(/storage/emulated/0)无法创建文件夹
- AndroidManifest.xml文件详解(uses-feature)
- android 视频图片混合轮播实现
- Android Studio 导入so文件
- android用ImageView显示网络图片
- Android下MP3播放器的实现源代码02
- android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出