【需求】app项目中由于使用了全屏沉浸式,造成整个布局上移,所以自定了标题,将标题向下移。
/**
* @Author Lee
* @Time 2018/9/14
* @Theme
*/
public class CustomTilte extends RelativeLayout {
private int statusBarHeight;
private OnBackClickListener mListener;
private String title;
private String mTitle;
private TextView mTvTitle;
private boolean isBackDismiss;
private ImageView mIVBack;
public CustomTilte(Context context) {
this(context , null);
}
public CustomTilte(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs ,0);
}
public CustomTilte(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(final Context context, AttributeSet attrs, int defStyle) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppTilte, defStyle, 0);
title = a.getString(R.styleable.AppTilte_title);
isBackDismiss = a.getBoolean(R.styleable.AppTilte_isBackDismiss, false);
a.recycle();
Log.v("lee", "------------- 自定义标题 ---------------- " + title);
View view = LayoutInflater.from(context).inflate(R.layout.include_title, this);
mTvTitle = view.findViewById(R.id.tv_title);
mIVBack = view.findViewById(R.id.iv_back);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
statusBarHeight = MachineUtils.getStatusBarHeight(getContext());
mTvTitle.setPadding(0, statusBarHeight + 10, 0, 0);
mIVBack.setPadding(0,statusBarHeight + 10,0,0);
}
mTvTitle.setText(title);
if(isBackDismiss){
mIVBack.setVisibility(GONE);
}else {
mIVBack.setVisibility(VISIBLE);
mIVBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mListener != null){
mListener.onBack();
}
}
});
}
}
public void setTitle(String title){
mTvTitle.setText(title);
}
public interface OnBackClickListener{
void onBack();
}
public void setOnBackListener(OnBackClickListener mListener) {
this.mListener = mListener;
}
}
【xml布局】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:id="@+id/rl_title"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="标题"
android:layout_centerInParent="true"
android:textColor="@color/shell_font_black"
android:textStyle="bold"
android:textSize="20sp"/>
<ImageView
android:id="@+id/iv_back"
android:src="@drawable/vest_icon_back"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
【attrs.xml】
<declare-styleable name="AppTilte">
<attr name="title" format="string" /> <!-- 标题 -->
<attr name="isBackDismiss" format="boolean"/> <!-- 返回键 是否可见-->
</declare-styleable>
图片.png
【工具类】
public class MachineUtils {
/**
* 隐藏输入键盘的方法
*/
public static void hideInput(Activity activity) {
if (activity == null) {
return;
}
InputMethodManager imm = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(activity.getWindow().getDecorView()
.getWindowToken(), 0);
}
}
/**
* 显示输入键盘的方法
*/
public static void showInput(Context context, View view) {
if (view != null) {
view.requestFocus();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, 0);
}
}
/**
* 切换输入键盘的方法
*/
public static void toggleInput(Context context) {
if (context == null) {
return;
}
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 测量下方操作栏的高度
* @param context
* @return
*/
public static int getNavigationBarHeight(Context context) {
if (!hasSoftKeys((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))) {
return 0;
}
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
return resources.getDimensionPixelSize(resourceId);
}
/**
* 测量状态栏的高度
* @param context
* @return
*/
public static int getStatusBarHeight(Context context){
if (context == null){
return -1;
}
int statusBarHeight = -1;
//获取status_bar_height资源的ID
int resourceId =context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
//根据资源ID获取响应的尺寸值
statusBarHeight = context. getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
private static boolean hasSoftKeys(WindowManager windowManager) {
Display d = windowManager.getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
d.getRealMetrics(realDisplayMetrics);
int realHeight = realDisplayMetrics.heightPixels;
int realWidth = realDisplayMetrics.widthPixels;
DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;
return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}
}
【使用】
(1)在布局中:
<com.shuixin.mvpshell.widget.CustomTilte
android:id="@+id/custom_title"
AppTilte:title = "头条资讯"
android:layout_width="match_parent"
android:layout_height="@dimen/shell_actionbar_height"/>
(2)在代码中:
CustomTitle mCustomTilte = (CustomTilte) findViewById(R.id.custom_title);
mCustomTilte.setOnBackListener(new CustomTilte.OnBackClickListener() {
@Override
public void onBack() {
finish();
}
});
网友评论