設置工具類android
public class ImageTextButton extends android.support.v7.widget.AppCompatTextView { private Drawable drawableLeft; private int scaleWidth; //dp值 private int scaleHeight; public ImageTextButton(Context context) { super(context); } public ImageTextButton(Context context, AttributeSet attrs) { super(context, attrs); init(context,attrs); } public ImageTextButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } public void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageTextButton); drawableLeft = typedArray.getDrawable(R.styleable.ImageTextButton_leftDrawable); scaleWidth = typedArray.getDimensionPixelOffset(R.styleable .ImageTextButton_drawableWidth, UIUtils.dp2px(getContext(),20)); scaleHeight = typedArray.getDimensionPixelOffset(R.styleable .ImageTextButton_drawableHeight, UIUtils.dp2px(getContext(),20)); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (drawableLeft != null) { drawableLeft.setBounds(0, 0, UIUtils.dp2px(getContext(),scaleWidth), UIUtils.dp2px(getContext(),scaleHeight)); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); this.setCompoundDrawables(drawableLeft, null, null, null); } /** * 設置左側圖片並重繪 * @param drawableLeft */ public void setDrawableLeft(Drawable drawableLeft) { this.drawableLeft = drawableLeft; invalidate(); } /** * 設置左側圖片並重繪 * @param drawableLeftRes */ public void setDrawableLeft(int drawableLeftRes) { this.drawableLeft = UIUtils.getContext().getResources().getDrawable(drawableLeftRes); invalidate(); } }
附上attrs:canvas
<!-- ImageTextButton attrs--> <attr name="leftDrawable" format="reference"/> <attr name="drawableWidth" format="dimension"/> <attr name="drawableHeight" format="dimension"/> <declare-styleable name="ImageTextButton"> <attr name="leftDrawable"/> <attr name="drawableWidth"/> <attr name="drawableHeight"/> </declare-styleable>
最後在xml中調用便可,讓咱們看一下效果數組
<com.guorentong.learn.organ.utils.ImageTextButton android:layout_width="match_parent" android:layout_height="wrap_content" android:drawablePadding="5dp" android:text="課程時長" android:textColor="@color/colorText" android:textSize="30px" app:drawableHeight="20px" app:drawableWidth="6px" app:leftDrawable="@drawable/lantiao" />
尺寸轉化類app
public class UIUtils { public static Context getContext() { return MyApp.getInstance(); } /** * 獲取資源對象 */ public static Resources getResources() { return getContext().getResources(); } /** * 獲取資源文件字符串 * * @param id * @return */ public static String getString(int id) { return getResources().getString(id); } /** * 獲取資源文件字符串數組 * * @param id * @return */ public static String[] getStringArray(int id) { return getResources().getStringArray(id); } /** * 獲取資源文件圖片 * * @param id * @return */ public static Drawable getDrawable(int id) { return ContextCompat.getDrawable(getContext(), id); } /** * 獲取資源文件顏色 * * @param id * @return */ public static int getColor(int id) { return ContextCompat.getColor(getContext(), id); } /** * 根據id獲取顏色的狀態選擇器 * * @param id * @return */ public static ColorStateList getColorStateList(int id) { return ContextCompat.getColorStateList(getContext(), id); } /** * 獲取尺寸 * * @param id * @return */ public static int getDimen(int id) { return getResources().getDimensionPixelSize(id); // 返回具體像素值 } /** * dp轉px */ public static int dp2px(Context context, float dpVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources().getDisplayMetrics()); } /** * px -> dp * * @param px * @return */ public static float px2dp(int px) { float density = getResources().getDisplayMetrics().density; return px / density; } /** * 加載佈局文件 * * @param id * @return */ public static View inflate(int id) { return View.inflate(getContext(), id, null); } /** * 把自身從父View中移除 * @param view */ public static void removeSelfFromParent(View view) { if (view != null) { ViewParent parent = view.getParent(); if (parent != null && parent instanceof ViewGroup) { ViewGroup group = (ViewGroup) parent; group.removeView(view); } } } /** * 請求View樹從新佈局,用於解決中層View有佈局狀態而致使上層View狀態斷裂 * @param view * @param isAll */ public static void requestLayoutParent(View view, boolean isAll) { ViewParent parent = view.getParent(); while (parent != null && parent instanceof View) { if (!parent.isLayoutRequested()) { parent.requestLayout(); if (!isAll) { break; } } parent = parent.getParent(); } } /** * 判斷觸點是否落在該View上 * @param ev * @param v * @return */ public static boolean isTouchInView(MotionEvent ev, View v) { int[] vLoc = new int[2]; v.getLocationOnScreen(vLoc); float motionX = ev.getRawX(); float motionY = ev.getRawY(); return motionX >= vLoc[0] && motionX <= (vLoc[0] + v.getWidth()) && motionY >= vLoc[1] && motionY <= (vLoc[1] + v.getHeight()); } /** * findViewById的泛型封裝,減小強轉代碼 * @param layout * @param id * @param <T> * @return */ public static <T extends View> T findViewById(View layout, int id) { return (T) layout.findViewById(id); } /** * 獲取屏幕的比例 * @param context * @return */ public static float getScaledDensity(Context context) { DisplayMetrics dm = context.getResources().getDisplayMetrics(); float value = dm.scaledDensity; return value; } /** * 獲取控件的高度,若是獲取的高度爲0,則從新計算尺寸後再返回高度 * @param view * @return */ public static int getViewMeasuredHeight(View view) { calcViewMeasure(view); return view.getMeasuredHeight(); } /** * 獲取控件的寬度,若是獲取的寬度爲0,則從新計算尺寸後再返回寬度 * @param view * @return */ public static int getViewMeasuredWidth(View view) { calcViewMeasure(view); return view.getMeasuredWidth(); } /** * 測量控件的尺寸 * @param view */ public static void calcViewMeasure(View view) { int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int expandSpec = View.MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST); view.measure(width, expandSpec); } /** * 設置textview指定文字爲某一顏色 * * @param content 顯示的文字 * @param color 須要轉換成的顏色值 * @param start 須要變色文字開始位置 * @param end 須要變色文字結束位置 */ public static SpannableStringBuilder changeTextColor(String content, int color, int start, int end) { SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content); spannableStringBuilder.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return spannableStringBuilder; } }
drawable樣式文件ide
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <!--顏色--> <solid android:color="@color/colorAccent"/> <!--內邊距--> <!--<padding--> <!--android:bottom="20dp"--> <!--android:left="20dp"--> <!--android:right="20dp"--> <!--android:top="20dp"/>--> <corners android:topLeftRadius="10dp" android:topRightRadius="10dp" android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/> <!--設置圓角--> <corners android:radius="10dip"/> <!--stroke設置描邊--> <!--<stroke--> <!--android:width="2dp"--> <!--android:color="@android:color/darker_gray"/>--> </shape>