通用組合式控制元件---標題欄(支援copy)
阿新 • • 發佈:2018-12-31
直接上程式碼;
package com.example.lenovo.demo; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.constraint.ConstraintLayout; import android.support.v4.content.ContextCompat; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; import android.widget.TextView; public class TopTitle extends FrameLayout { private TextView tvTitle; private TextView tvBack; private TextView tvMenu; private String title; private String back; private String menu; private Drawable backIcon; private Drawable menuIcon; private Drawable menuBg; private boolean noBack; public TopTitle(@NonNull Context context) { super(context); init(null,0); } public TopTitle(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(attrs,0); } public TopTitle(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs,defStyleAttr); } private void init(AttributeSet attrs, int defStyleAttr) { @SuppressLint("Recycle") final TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.TopTitle,defStyleAttr,0); title = array.getString(R.styleable.TopTitle_title); back = array.getString(R.styleable.TopTitle_backName); menu = array.getString(R.styleable.TopTitle_menuName); noBack = array.getBoolean(R.styleable.TopTitle_noBack,false); if (array.hasValue(R.styleable.TopTitle_backIcon)){ backIcon = array.getDrawable(R.styleable.TopTitle_backIcon); } if (array.hasValue(R.styleable.TopTitle_menuIcon)){ menuIcon = array.getDrawable(R.styleable.TopTitle_menuIcon); } if (array.hasValue(R.styleable.TopTitle_menuBg)){ menuBg = array.getDrawable(R.styleable.TopTitle_menuBg); } array.recycle(); if (backIcon == null) backIcon = ContextCompat.getDrawable(getContext(),R.mipmap.back_white); View.inflate(getContext(),R.layout.title_normal_layout,this); tvTitle = findViewById(R.id.tvTitle); tvBack = findViewById(R.id.tvBack); tvMenu = findViewById(R.id.tvMenu); tvBack.setVisibility(noBack?GONE:VISIBLE); tvTitle.setText(title); tvBack.setText(back); tvMenu.setText(menu); if (menuIcon != null){ tvMenu.setCompoundDrawablesRelativeWithIntrinsicBounds(menuIcon,null,null,null); tvMenu.setCompoundDrawablePadding(10); } if (menuBg != null){ tvMenu.setBackground(menuBg); tvMenu.setPadding(ScreenUtils.dp2px(8),ScreenUtils.dp2px(6),ScreenUtils.dp2px(8),ScreenUtils.dp2px(6)); ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) tvMenu.getLayoutParams(); params.rightMargin = ScreenUtils.dp2px(10); tvMenu.setLayoutParams(params); } tvBack.setCompoundDrawablesRelativeWithIntrinsicBounds(backIcon,null,null,null); tvBack.setCompoundDrawablePadding(10); } public void setTitle(String title) { tvTitle.setText(title); } public void setBack(String back) { tvBack.setText(back); } public void setMenu(String menu) { tvMenu.setText(menu); } public void setOnBackListener(OnClickListener listener){ tvBack.setOnClickListener(listener); } public void setOnMenuListener(OnClickListener listener){ tvMenu.setOnClickListener(listener); } public TextView getTvTitle() { return tvTitle; } }
尺寸工具類;
package com.example.lenovo.demo; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Rect; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; import java.lang.reflect.Field; /** * 獲得螢幕相關的輔助類 */ public class ScreenUtils { private ScreenUtils() { /* cannot be instantiated */ throw new UnsupportedOperationException("cannot be instantiated"); } /** * 獲得螢幕寬度 * * @param context * @return */ public static int getScreenWidth( Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * 獲得螢幕高度 * * @param context * @return */ public static int getScreenHeight( Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; } /** * 獲得狀態列的高度 * * @param context * @return */ public static int getStatusHeight( Context context) { int statusHeight = -1; try { Class<?> clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); int height = Integer.parseInt(clazz.getField("status_bar_height") .get(object).toString()); statusHeight = context.getResources().getDimensionPixelSize(height); } catch (Exception e) { e.printStackTrace(); } return statusHeight; } /** * 獲取當前螢幕截圖,包含狀態列 * * @param activity * @return */ public static Bitmap snapShotWithStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, 0, width, height); view.destroyDrawingCache(); return bp; } /** * 獲取當前螢幕截圖,不包含狀態列 * * @param activity * @return */ public static Bitmap snapShotWithoutStatusBar(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = getScreenWidth(activity); int height = getScreenHeight(activity); Bitmap bp = null; bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bp; } private static double RATIO = 0.85; public static int screenWidth; public static int screenHeight; public static int screenMin;// 寬高中,小的一邊 public static int screenMax;// 寬高中,較大的值 public static float density; public static float scaleDensity; public static float xdpi; public static float ydpi; public static int densityDpi; public static int dialogWidth; public static int statusbarheight; public static int navbarheight; public static int dip2px(float dipValue) { return (int) (dipValue * density + 0.5f); } public static int px2dip(float pxValue) { return (int) (pxValue / density + 0.5f); } public static int getDialogWidth() { dialogWidth = (int) (screenMin * RATIO); return dialogWidth; } public static void init( Context context) { if (null == context) { return; } DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; screenMin = (screenWidth > screenHeight) ? screenHeight : screenWidth; density = dm.density; scaleDensity = dm.scaledDensity; xdpi = dm.xdpi; ydpi = dm.ydpi; densityDpi = dm.densityDpi; } public static int getDisplayHeight(Context context) { if (screenHeight == 0) { GetInfo(context); } return screenHeight; } private static void GetInfo( Context context) { if (null == context) { return; } DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; screenMin = (screenWidth > screenHeight) ? screenHeight : screenWidth; screenMax = (screenWidth < screenHeight) ? screenHeight : screenWidth; density = dm.density; scaleDensity = dm.scaledDensity; xdpi = dm.xdpi; ydpi = dm.ydpi; densityDpi = dm.densityDpi; statusbarheight = getStatusBarHeight(context); navbarheight = getNavBarHeight(context); } private static int getStatusBarHeight( Context context) { Class<?> c = null; Object obj = null; Field field = null; int x = 0, sbar = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); x = Integer.parseInt(field.get(obj).toString()); sbar = context.getResources().getDimensionPixelSize(x); } catch (Exception E) { E.printStackTrace(); } return sbar; } private static int getNavBarHeight( Context context) { Resources resources = context.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { return resources.getDimensionPixelSize(resourceId); } return 0; } public static int dp2px(float dp) { return (int) (dp * density + 0.5f); } }
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TopTitle"> <attr name="backIcon"/> <attr name="backName" /> <attr name="noBack" /> <attr name="title" /> <attr name="menuIcon" /> <attr name="menuName"/> <attr name="menuBg"/> </declare-styleable> </resources>
xml中的用法;
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.lenovo.demo.TopTitle
app:title="@string/app_name"
android:id="@+id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="0dp"
android:layout_height="wrap_content">
</com.example.lenovo.demo.TopTitle>
</android.support.constraint.ConstraintLayout>
java中的使用;
TopTitle tvTitle = findViewById(R.id.title);
tvTitle.setOnBackListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
以上程式碼可以直接copy使用,非常方便!