ソースを参照

CommonUserItemView.java MyTitleView.java修改

niusongtao@dl-cg.com 5 年 前
コミット
bcee3015ff

+ 8 - 0
base_library/src/main/java/com/mgtech/base_library/custom/CommonUserItemView.java

@@ -161,6 +161,14 @@ public class CommonUserItemView extends ConstraintLayout {
         titleText.setCompoundDrawablePadding(padding);
     }
 
+    public void setTitleTextDrawableRight(Drawable drawable,int padding){
+        if (drawable == null)
+            return;
+        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
+        titleText.setCompoundDrawables(null,null,drawable,null);
+        titleText.setCompoundDrawablePadding(padding);
+    }
+
     public void setTitleText(String title){
         titleText.setText(title);
     }

+ 12 - 0
base_library/src/main/java/com/mgtech/base_library/custom/MyTitleView.java

@@ -3,6 +3,7 @@ package com.mgtech.base_library.custom;
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.graphics.Color;
+import android.graphics.Typeface;
 import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
 import android.util.TypedValue;
@@ -74,6 +75,8 @@ public class MyTitleView extends ConstraintLayout implements View.OnClickListene
         TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyTitleView,defStyleAttr,0);
         bgLayout.setBackgroundColor(typedArray.getColor(R.styleable.MyTitleView_title_bg_color, Color.WHITE));
         titleText.setText(typedArray.getText(R.styleable.MyTitleView_title_text));
+        titleText.setTextSize(TypedValue.COMPLEX_UNIT_PX,typedArray.getDimension(R.styleable.MyTitleView_title_text_size
+                ,titleText.getTextSize()));
         rightText.setText(typedArray.getText(R.styleable.MyTitleView_title_right_text));
         rightText.setTextColor(typedArray.getColor(R.styleable.MyTitleView_title_right_text_color,Color.BLACK));
         rightText.setTextSize(TypedValue.COMPLEX_UNIT_PX,typedArray.getDimension(R.styleable.MyTitleView_title_right_text_size
@@ -83,6 +86,15 @@ public class MyTitleView extends ConstraintLayout implements View.OnClickListene
         titleText.setTextColor(typedArray.getColor(R.styleable.MyTitleView_title_text_color, Color.BLACK));
         centerImage.setBackground(typedArray.getDrawable(R.styleable.MyTitleView_center_image));
         setConstraintSet(typedArray.getInt(R.styleable.MyTitleView_title_text_gravity,0));
+
+        int type = typedArray.getInt(R.styleable.MyTitleView_title_text_style,10);
+        if (type == 10){//normal
+            titleText.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
+        } else if (type == 20){//bold
+            titleText.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
+        } else if (type == 30){//italic
+            titleText.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
+        }
         typedArray.recycle();
         leftImage.setOnClickListener(this);
         rightImage.setOnClickListener(this);

+ 12 - 0
base_library/src/main/java/com/mgtech/base_library/util/SpannableStringUtil.java

@@ -87,6 +87,18 @@ public class SpannableStringUtil {
         return spannableString;
     }
 
+    /**
+     * 给文本添加删除线
+     * @param text 文本
+     * @return 处理后文本
+     */
+    public static SpannableString strikeThroughSpan(String text){
+        SpannableString spannableString = new SpannableString(text);
+        StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
+        spannableString.setSpan(strikethroughSpan,0,text.length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
+        return spannableString;
+    }
+
 
     /**
      * 给文本设置样式和绝对大小字体

+ 6 - 0
base_library/src/main/res/values/attrs.xml

@@ -4,6 +4,7 @@
         <attr name="title_bg_color" format="color"/>
         <attr name="title_text_color" format="color"/>
         <attr name="title_text" format="string"/>
+        <attr name="title_text_size" format="dimension"/>
         <attr name="title_right_text" format="string"/>
         <attr name="left_image" format="reference"/>
         <attr name="right_image" format="reference"/>
@@ -14,6 +15,11 @@
             <flag name="left" value="0" />
             <flag name="center" value="1" />
         </attr>
+        <attr name="title_text_style">
+            <flag name="normal" value="10" />
+            <flag name="bold" value="20" />
+            <flag name="italic" value="30" />
+        </attr>
     </declare-styleable>
 
     <declare-styleable name="SwitchView">