每个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);
}
}
布局文件 这里我们都将图片隐藏掉
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="end"
android:gravity="center"
android:paddingLeft="60dp"
android:paddingTop="13dp"
android:paddingRight="60dp"
android:paddingBottom="13dp"
android:singleLine="true"
android:textColor="@color/common_text_color"
android:textSize="18sp"
tools:text="@string/app_name" />
<ImageButton
android:id="@+id/title_back"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:background="@null"
android:padding="10dp"
android:src="@drawable/ic_back" />
<ImageButton
android:id="@+id/title_share"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="@null"
android:padding="10dp"
android:src="@drawable/ic_share"
android:visibility="gone" />
<ImageButton
android:id="@+id/title_rule"
android:layout_width="@dimen/dp_18"
android:layout_height="@dimen/dp_18"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dp_20"
android:background="@null"
android:visibility="gone"
android:src="@drawable/ic_invalid_name" />
<View
android:id="@+id/bottom_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignBottom="@id/title_tv"
android:background="#e5e5e5"></View>
</merge>
attrs文件
<declare-styleable name="TitleBar">
<attr name="enable_share" format="boolean" />
<attr name="title_color" format="reference" />
<attr name="back_icon" format="reference" />
<attr name="share_icon" format="reference" />
<attr name="title_text" format="reference" />
<attr name="show_bottom_line" format="boolean" />
<attr name="integral_rule" format="reference" />
<attr name="enable_rule" format="boolean" />
</declare-styleable>
好啦 现在代码是写完了,那么我们怎么用呢? 听我慢慢讲解,比如我们的状态栏里有分享 如下图
那么我们直接
把我们这个属性为true就可以了。