com.hebky.xtdz.weight.CommonTopBar android:id="@+id/address_book_combar_topbar" android:layout_width="match_parent" android:layout_height="@dimen/common_top_bar_height" android:background="@color/weight_bg_commom_top_bar" /import android.an
android:id="@+id/address_book_combar_topbar"
android:layout_width="match_parent"
android:layout_height="@dimen/common_top_bar_height"
android:background="@color/weight_bg_commom_top_bar" />import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
/**
* Created by wangyunpeng on 2016/11/1.
* common top bar
*/
public class CommonTopBar extends FrameLayout {
private Context mContext;
private TextView mLeftTextView;//left view
private CommonTopBarClick mCommonTopBarClick;
private TextView mMidTextView;//middle view
private TextView mRightTextView;//right view
private ImageView mRightImg;
private ImageView mLeftImg;
/**
* @return 左侧图片是否显示
*/
public boolean isLeftImgIsVisible() {
return mLeftImg != null && mLeftImg.getVisibility() == View.VISIBLE;
}
/**
* @return 左侧文字是否显示
*/
public boolean isLeftViewIsVisible() {
return mLeftTextView != null && mLeftTextView.getVisibility() == View.VISIBLE;
}
/**
* @return 右侧图片是否显示
*/
public boolean isRightImgIsVisible() {
return mRightImg != null && mRightImg.getVisibility() == View.VISIBLE;
}
/**
* @return 右侧文字是否显示
*/
public boolean isRightViewIsVisible() {
return mRightTextView != null && mRightTextView.getVisibility() == View.VISIBLE;
}
public CommonTopBar(Context context) {
this(context, null);
}
public CommonTopBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
initView();
}
public CommonTopBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* init view
*/
@SuppressLint("InflateParams")
private void initView() {
View view = ((Activity) mContext).getLayoutInflater().inflate(R.layout.weight_common_bar_layout, null);
mLeftTextView = view.findViewById(R.id.common_top_bar_text_left);
mLeftTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mCommonTopBarClick != null)
mCommonTopBarClick.onClickLeft();
}
});
mMidTextView = view.findViewById(R.id.common_top_bar_text_mid);
mRightTextView = view.findViewById(R.id.common_top_bar_text_right);
mRightImg = view.findViewById(R.id.common_top_bar_img_right);
mLeftImg = view.findViewById(R.id.common_top_bar_img_left);
mLeftImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mCommonTopBarClick != null)
mCommonTopBarClick.onClickLeft();
}
});
mRightImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mCommonTopBarClick != null)
mCommonTopBarClick.onClickRight();
}
});
mRightTextView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mCommonTopBarClick != null)
mCommonTopBarClick.onClickRight();
}
});
addView(view);
LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
view.setLayoutParams(layoutParams);
}
/**
* if show right view,default gone
*
* @param isShow
*/
public void isShowRightView(boolean isShow) {
if (mRightImg != null && isShow)
mRightImg.setVisibility(View.GONE);
if (mRightTextView != null)
mRightTextView.setVisibility(isShow ? View.VISIBLE : View.GONE);
}
public void isShowRightImgView(boolean isShow) {
if (mRightTextView != null && isShow)
mRightTextView.setVisibility(View.GONE);
if (mRightImg != null)
mRightImg.setVisibility(isShow ? View.VISIBLE : View.GONE);
}
/**
* 设if show left view,default gone
*
* @param isShow
*/
public void isShowLeftView(boolean isShow) {
if (mLeftImg != null && isShow)
mLeftImg.setVisibility(View.GONE);
if (mLeftTextView != null)
mLeftTextView.setVisibility(isShow ? View.VISIBLE : View.GONE);
}
public void setLeftTextBold(boolean isBold) {
if (mLeftTextView != null)
mLeftTextView.getPaint().setFakeBoldText(isBold);
}
public void isShowLeftImgView(boolean isShow) {
if (mLeftTextView != null && isShow)
mLeftTextView.setVisibility(View.GONE);
if (mLeftImg != null)
mLeftImg.setVisibility(isShow ? View.VISIBLE : View.GONE);
}
/*public CommonTopBarClick getCommonTopBarClick() {
return mCommonTopBarClick;
}*/
/**
* set click event listener
*
* @param mCommonTopBarClick
*/
public void setCommonTopBarClick(CommonTopBarClick mCommonTopBarClick) {
this.mCommonTopBarClick = mCommonTopBarClick;
}
/**
* set right view margin right,the param is dp
*
* @param marginRight
*/
public void addRightMargin(float marginRight) {
if (mRightTextView != null) {
RelativeLayout.LayoutParams mLayoutParams = (RelativeLayout.LayoutParams) mRightTextView.getLayoutParams();
mLayoutParams.rightMargin = dp2px(mContext, marginRight);
mRightTextView.setLayoutParams(mLayoutParams);
mRightTextView.requestLayout();
}
}
public void addRightImgMargin(float marginRight) {
if (mRightImg != null) {
RelativeLayout.LayoutParams mLayoutParams = (RelativeLayout.LayoutParams) mRightImg.getLayoutParams();
mLayoutParams.rightMargin = dp2px(mContext, marginRight);
mRightImg.setLayoutParams(mLayoutParams);
mRightImg.requestLayout();
}
}
/**
* set left view margin left,the param is dp
*
* @param marginLeft
*/
public void addLeftMargin(float marginLeft) {
if (mLeftTextView != null) {
RelativeLayout.LayoutParams mLayoutParams = (RelativeLayout.LayoutParams) mLeftTextView.getLayoutParams();
mLayoutParams.leftMargin = dp2px(mContext, marginLeft);
mLeftTextView.setLayoutParams(mLayoutParams);
mLeftTextView.requestLayout();
}
}
public void addLeftImgMargin(float marginLeft) {
if (mLeftImg != null) {
RelativeLayout.LayoutParams mLayoutParams = (RelativeLayout.LayoutParams) mLeftImg.getLayoutParams();
mLayoutParams.leftMargin = dp2px(mContext, marginLeft);
mLeftImg.setLayoutParams(mLayoutParams);
mLeftImg.requestLayout();
}
}
/**
* set
*
* @param lenth
*/
public void setMidTextMaxLenth(int lenth) {
if (mMidTextView != null)
mMidTextView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(lenth)});
}
/**
* set mid view text
*
* @param charSequence
*/
public void setMidText(CharSequence charSequence) {
if (mMidTextView != null)
mMidTextView.setText(charSequence);
}
public void setMidTextBold(boolean isBold) {
if (mMidTextView != null)
mMidTextView.getPaint().setFakeBoldText(isBold);
}
/**
* set mid view text
*
* @param textRes
*/
public void setMidText(@StringRes int textRes) {
if (mMidTextView != null)
mMidTextView.setText(textRes);
}
/**
* set mid text size
*
* @param size
*/
public void setMidTextSize(float size) {
if (mMidTextView != null)
mMidTextView.setTextSize(size);
}
/**
* set mid text color
*
* @param colorStateList
*/
public void setMidTextColor(@NonNull ColorStateList colorStateList) {
if (mMidTextView != null)
mMidTextView.setTextColor(colorStateList);
}
/**
* set mid text color
*
* @param color
*/
public void setMidTextColor(@ColorInt int color) {
if (mMidTextView != null)
mMidTextView.setTextColor(color);
}
/**
* set left image
*
* @param drawable
*/
public void setLeftImage(@NonNull Drawable drawable) {
if (mLeftImg != null) {
if (Build.VERSION.SDK_INT < 16) {
mLeftImg.setBackgroundDrawable(drawable);
} else {
mLeftImg.setBackground(drawable);
}
}
}
/**
* set left image
*
* @param drawableRes
*/
public void setLeftImage(@DrawableRes int drawableRes) {
if (mLeftImg != null)
mLeftImg.setBackgroundResource(drawableRes);
}
/**
* set left view text
*
* @param charSequence
*/
public void setLeftText(CharSequence charSequence) {
if (mLeftTextView != null)
mLeftTextView.setText(charSequence);
}
/**
* set left view text
*
* @param textRes
*/
public void setLeftText(@StringRes int textRes) {
if (mLeftTextView != null)
mLeftTextView.setText(textRes);
}
/**
* set left text size
*
* @param size
*/
public void setLeftTextSize(float size) {
if (mLeftTextView != null)
mLeftTextView.setTextSize(size);
}
/**
* set left text color
*
* @param colorStateList
*/
public void setLeftTextColor(@NonNull ColorStateList colorStateList) {
if (mLeftTextView != null)
mLeftTextView.setTextColor(colorStateList);
}
/**
* set left text color
*
* @param color
*/
public void setLeftTextColor(@ColorInt int color) {
if (mLeftTextView != null)
mLeftTextView.setTextColor(color);
}
/**
* set right image
*
* @param drawable
*/
public void setRightImage(@NonNull Drawable drawable) {
if (mRightImg != null) {
if (Build.VERSION.SDK_INT < 16) {
mRightImg.setBackgroundDrawable(drawable);
} else {
mRightImg.setBackground(drawable);
}
}
}
/**
* set right image
*
* @param drawableRes
*/
public void setRightImage(@DrawableRes int drawableRes) {
if (mRightImg != null)
mRightImg.setBackgroundResource(drawableRes);
}
/**
* set right view text
*
* @param charSequence
*/
public void setRightText(CharSequence charSequence) {
if (mRightTextView != null)
mRightTextView.setText(charSequence);
}
public void setRightTextBold(boolean isBold) {
if (mRightTextView != null)
mRightTextView.getPaint().setFakeBoldText(isBold);
}
/**
* set right view text
*
* @param textRes
*/
public void setRightText(@StringRes int textRes) {
if (mRightTextView != null)
mRightTextView.setText(textRes);
}
/**
* set right text size
*
* @param size
*/
public void setRightTextSize(float size) {
if (mRightTextView != null)
mRightTextView.setTextSize(size);
}
/**
* set right text color
*
* @param colorStateList
*/
public void setRightTextColor(@NonNull ColorStateList colorStateList) {
if (mRightTextView != null)
mRightTextView.setTextColor(colorStateList);
}
/**
* set right text color
*
* @param color
*/
public void setRightTextColor(@ColorInt int color) {
if (mRightTextView != null)
mRightTextView.setTextColor(color);
}
/**
* set DrawPadding
*
* @param size dp
*/
public void setLefTextViewDrawPadding(int size) {
if (mLeftTextView != null)
mLeftTextView.setCompoundDrawablePadding(dp2px(mContext, size));
}
/**
* set left drawImg
*
* @param drawableRes
*/
public void setLeftTextViewDrawLeft(@DrawableRes int drawableRes) {
if (mLeftTextView != null)
mLeftTextView.setCompoundDrawablesWithIntrinsicBounds(drawableRes, 0, 0, 0);
}
/**
* set left drawImg
*
* @param drawableRes
*/
public void setLeftTextViewDrawLeft(@NonNull Drawable drawableRes) {
if (mLeftTextView != null)
mLeftTextView.setCompoundDrawablesWithIntrinsicBounds(drawableRes, null, null, null);
}
/*转换方法*/
/**
* dp转px
*
* @param context
* @param dpVal
* @return
*/
public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics());
}
/**
* sp转px
*
* @param context
* @param spVal
* @return
*/
public static int sp2px(Context context, float spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, context.getResources().getDisplayMetrics());
}
/**
* px转dp
*
* @param context
* @param pxVal
* @return
*/
public static float px2dp(Context context, float pxVal) {
final float scale = context.getResources().getDisplayMetrics().density;
return (pxVal / scale);
}
/**
* px转sp
*
* @param context
* @param pxVal
* @return
*/
public static float px2sp(Context context, float pxVal) {
return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
}
/*接口*/
/**
* Created by wangyunpeng on 2016/3/11.
*/
public interface CommonTopBarClick {
void onClickLeft();
void onClickRight();
}
/*
* 布局:weight_common_bar_layout.xml
*
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/common_top_bar_text_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:gravity="center"
android:textColor="@color/white"
android:textSize="20sp"
android:visibility="gone" />
<ImageView
android:id="@+id/common_top_bar_img_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:visibility="gone" />
<TextView
android:id="@+id/common_top_bar_text_mid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="end"
android:gravity="center"
android:maxLength="8"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/common_top_bar_text_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:gravity="center"
android:paddingRight="10dp"
android:textColor="@color/white"
android:textSize="20sp"
android:visibility="gone" />
<ImageView
android:id="@+id/common_top_bar_img_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
* */
}
weight_common_bar_layout.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/common_top_bar_text_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackgroundBorderless"
android:gravity="center"
android:textColor="@color/white"
android:textSize="20sp"
android:visibility="gone" />
<ImageView
android:id="@+id/common_top_bar_img_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:visibility="gone" />
<TextView
android:id="@+id/common_top_bar_text_mid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="end"
android:gravity="center"
android:maxLength="8"
android:maxLines="1"
android:singleLine="true"
android:textColor="@color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/common_top_bar_text_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:gravity="center"
android:paddingRight="10dp"
android:textColor="@color/white"
android:textSize="20sp"
android:visibility="gone" />
<ImageView
android:id="@+id/common_top_bar_img_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone" />
</RelativeLayout>
<color name="weight_bg_commom_top_bar">#367ee4</color><!--淡灰色 -->