• 安卓自定义设置类View


    效果图

      1 import android.content.Context;
      2 import android.content.res.TypedArray;
      3 import android.graphics.drawable.Drawable;
      4 import android.util.AttributeSet;
      5 import android.view.LayoutInflater;
      6 import android.view.ViewGroup;
      7 import android.widget.ImageView;
      8 import android.widget.LinearLayout;
      9 import android.widget.RelativeLayout;
     10 import android.widget.TextView;
     11 
     12 import org.jetbrains.annotations.NotNull;
     13 
     14 /**
     15  * 自定义的设置类的view  类似于设置菜单
     16  * 可拓展
     17  *
     18  * @author Silence
     19  * @version 1.0
     20  * @since 1.0
     21  */
     22 public class CustomSettingView extends RelativeLayout {
     23 
     24     private RelativeLayout parentLayout;
     25 
     26     /**
     27      * 左侧image
     28      */
     29     private ImageView leftImageView;
     30 
     31     /**
     32      * 中间菜单
     33      */
     34     private TextView textView;
     35 
     36     /**
     37      * 右侧的textView 相当于带提示的那种
     38      */
     39     private TextView rightTextView;
     40 
     41     /**
     42      * 右侧image
     43      */
     44     private ImageView rightImageView;
     45 
     46     public CustomSettingView(Context context) {
     47         super(context);
     48     }
     49 
     50     public CustomSettingView(Context context, AttributeSet attrs) {
     51         super(context, attrs);
     52         init(context, attrs);
     53     }
     54 
     55     public CustomSettingView(Context context, AttributeSet attrs, int defStyleAttr) {
     56         super(context, attrs, defStyleAttr);
     57         init(context, attrs);
     58     }
     59 
     60     /**
     61      * 构造函数
     62      *
     63      * @param context 上下文
     64      */
     65     public void init(Context context, AttributeSet attrs) {
     66         //加载布局
     67         LayoutInflater.from(context).inflate(R.layout.custom_setting_view_layout, this, true);
     68 
     69         // 初始化控件
     70         parentLayout = findViewById(R.id.custom_setting_view_parent);
     71         leftImageView = findViewById(R.id.custom_setting_view_left_image);
     72         textView = findViewById(R.id.custom_setting_view_title);
     73         rightTextView = findViewById(R.id.custom_setting_view_right_title);
     74         rightImageView = findViewById(R.id.custom_setting_view_right_image);
     75 
     76         // 获取资源并设置
     77         TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomSettingView);
     78         // 左侧图片资源
     79         Drawable leftImageSrc = typedArray.getDrawable(R.styleable.CustomSettingView_custom_setting_view_left_src);
     80 
     81         // 中间菜单标题
     82         String textViewTitle = typedArray.getString(R.styleable.CustomSettingView_custom_setting_view_title);
     83         // 中间菜单标题大小
     84         float textViewTextSize = typedArray.getDimension(R.styleable.CustomSettingView_custom_setting_view_title_text_size, 12f);
     85         // 中间菜单标题颜色
     86         int textViewTextColor = typedArray.getColor(R.styleable.CustomSettingView_custom_setting_view_title_text_color, getResources().getColor(R.color.colorBlack));
     87         // 中间菜单标题左侧的距离
     88         float textViewMarginStart = typedArray.getDimension(R.styleable.CustomSettingView_custom_setting_view_title_margin_start, 0f);
     89         // 中间菜单标题右侧的距离
     90         float textViewMarginEnd = typedArray.getDimension(R.styleable.CustomSettingView_custom_setting_view_title_margin_end, 0f);
     91 
     92         // 右侧的textView的标题
     93         String rightTextTitle = typedArray.getString(R.styleable.CustomSettingView_custom_setting_view_right_title);
     94         // 右侧的textView的大小
     95         float rightTextSize = typedArray.getDimension(R.styleable.CustomSettingView_custom_setting_view_right_title_text_size, 12f);
     96         // 右侧的textView的颜色
     97         int rightTextColor = typedArray.getColor(R.styleable.CustomSettingView_custom_setting_view_right_title_text_color, getResources().getColor(R.color.colorBlack));
     98         // 右侧的textView MarginStart
     99         float rightTextMarginStart = typedArray.getDimension(R.styleable.CustomSettingView_custom_setting_view_right_title_margin_start, 0f);
    100         // 右侧的textView MarginEnd
    101         float rightTextMarginEnd = typedArray.getDimension(R.styleable.CustomSettingView_custom_setting_view_right_title_margin_end, 0f);
    102         // 右侧图片资源地址
    103         Drawable rightImageSrc = typedArray.getDrawable(R.styleable.CustomSettingView_custom_setting_view_right_src);
    104         boolean leftImageVisibility = typedArray.getBoolean(R.styleable.CustomSettingView_custom_setting_view_left_visibility, true);
    105         boolean rightImageVisibility = typedArray.getBoolean(R.styleable.CustomSettingView_custom_setting_view_right_visibility, true);
    106 
    107         if (leftImageSrc != null) {
    108             leftImageView.setImageDrawable(leftImageSrc);
    109         }
    110         leftImageView.setVisibility(leftImageVisibility ? VISIBLE : GONE);
    111 
    112         if (rightImageSrc != null) {
    113             rightImageView.setImageDrawable(rightImageSrc);
    114         }
    115         rightImageView.setVisibility(rightImageVisibility ? VISIBLE : GONE);
    116 
    117         textView.setText(textViewTitle);
    118         textView.getPaint().setTextSize(textViewTextSize);
    119         textView.setTextColor(textViewTextColor);
    120 
    121         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    122         layoutParams.setMargins((int) textViewMarginStart, 0, (int) textViewMarginEnd, 0);
    123         textView.setLayoutParams(layoutParams);
    124 
    125         rightTextView.setText(rightTextTitle);
    126         rightTextView.getPaint().setTextSize(rightTextSize);
    127         rightTextView.setTextColor(rightTextColor);
    128 
    129         LinearLayout.LayoutParams rightLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    130         rightLayoutParams.setMargins((int) rightTextMarginStart, 0, (int) rightTextMarginEnd, 0);
    131         rightTextView.setLayoutParams(rightLayoutParams);
    132 
    133         //释放资源
    134         typedArray.recycle();
    135     }
    136 
    137 
    138     public CustomSettingView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    139         super(context, attrs, defStyleAttr, defStyleRes);
    140     }
    141 
    142 
    143     public void setLeftImageSrc(@NotNull Drawable leftImageSrc) {
    144         leftImageView.setImageDrawable(leftImageSrc);
    145     }
    146 
    147     public void setTextViewTitle(String textViewTitle) {
    148         textView.setText(textViewTitle);
    149     }
    150 
    151     public void setTextViewTextSize(float textViewTextSize) {
    152         textView.getPaint().setTextSize(textViewTextSize);
    153     }
    154 
    155     public void setTextViewTextColor(@NotNull int textViewTextColor) {
    156         textView.setTextColor(textViewTextColor);
    157     }
    158 
    159     public void setTextViewMarginStart(float textViewMarginStart) {
    160         RelativeLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    161         layoutParams.setMarginStart((int) textViewMarginStart);
    162         textView.setLayoutParams(layoutParams);
    163     }
    164 
    165     public void setTextViewMarginEnd(float textViewMarginEnd) {
    166         RelativeLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    167         layoutParams.setMarginEnd((int) textViewMarginEnd);
    168         textView.setLayoutParams(layoutParams);
    169     }
    170 
    171     public void setRightTextMarginStart(float rightTextMarginStart) {
    172         RelativeLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    173         layoutParams.setMarginStart((int) rightTextMarginStart);
    174         rightTextView.setLayoutParams(layoutParams);
    175     }
    176 
    177     public void setRightTextMarginEnd(float rightTextMarginEnd) {
    178         RelativeLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    179         layoutParams.setMarginEnd((int) rightTextMarginEnd);
    180         rightTextView.setLayoutParams(layoutParams);
    181     }
    182 
    183     public void setRightTextTitle(String rightTextTitle) {
    184         rightTextView.setText(rightTextTitle);
    185     }
    186 
    187     public void setRightTextSize(float rightTextSize) {
    188         rightTextView.getPaint().setTextSize(rightTextSize);
    189     }
    190 
    191     public void setRightTextColor(int rightTextColor) {
    192         rightTextView.setTextColor(rightTextColor);
    193     }
    194 
    195     public void setRightImageSrc(@NotNull Drawable rightImageSrc) {
    196         rightImageView.setImageDrawable(rightImageSrc);
    197     }
    198 }
    View类
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:id="@+id/custom_setting_view_parent"
     4     android:layout_width="match_parent"
     5     android:layout_height="wrap_content">
     6 
     7     <LinearLayout
     8         android:id="@+id/custom_setting_view_left_part"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:layout_alignParentStart="true"
    12         android:layout_centerVertical="true"
    13         android:layout_toStartOf="@id/custom_setting_view_right_part"
    14         android:gravity="center_vertical"
    15         android:orientation="horizontal">
    16 
    17         <ImageView
    18             android:id="@+id/custom_setting_view_left_image"
    19             android:layout_width="wrap_content"
    20             android:layout_height="wrap_content" />
    21 
    22         <TextView
    23             android:id="@+id/custom_setting_view_title"
    24             android:layout_width="0dp"
    25             android:layout_height="wrap_content"
    26             android:layout_weight="1"
    27             android:ellipsize="end"
    28             android:singleLine="true" />
    29     </LinearLayout>
    30 
    31     <LinearLayout
    32         android:id="@+id/custom_setting_view_right_part"
    33         android:layout_width="wrap_content"
    34         android:layout_height="wrap_content"
    35         android:layout_alignParentEnd="true"
    36         android:layout_centerVertical="true"
    37         android:gravity="center_vertical|end"
    38         android:orientation="horizontal">
    39 
    40         <TextView
    41             android:id="@+id/custom_setting_view_right_title"
    42             android:layout_width="wrap_content"
    43             android:layout_height="wrap_content" />
    44 
    45         <ImageView
    46             android:id="@+id/custom_setting_view_right_image"
    47             android:layout_width="wrap_content"
    48             android:layout_height="wrap_content"
    49             android:src="@mipmap/arrow_right" />
    50 
    51     </LinearLayout>
    52 
    53 </RelativeLayout>
    XML文件
     1  <declare-styleable name="CustomSettingView">
     2         <!--        左侧图片资源-->
     3         <attr name="custom_setting_view_left_src" format="reference" />
     4         <!--        左侧图片资源是否显示-->
     5         <attr name="custom_setting_view_left_visibility" format="boolean" />
     6         <!--        标题-->
     7         <attr name="custom_setting_view_title" format="reference|string" />
     8         <!--        标题字体大小-->
     9         <attr name="custom_setting_view_title_text_size" format="dimension" />
    10         <!--        标题颜色-->
    11         <attr name="custom_setting_view_title_text_color" format="color" />
    12         <!--        标题左侧距离margin start-->
    13         <attr name="custom_setting_view_title_margin_start" format="dimension" />
    14         <!--        标题右侧距离margin end-->
    15         <attr name="custom_setting_view_title_margin_end" format="dimension" />
    16         <!--        右侧小标题-->
    17         <attr name="custom_setting_view_right_title" format="reference|string" />
    18         <!--        右侧小标题margin_start-->
    19         <attr name="custom_setting_view_right_title_margin_start" format="dimension" />
    20         <!--        右侧小标题margin_end-->
    21         <attr name="custom_setting_view_right_title_margin_end" format="dimension" />
    22         <!--        右侧小标题字体大小-->
    23         <attr name="custom_setting_view_right_title_text_size" format="dimension" />
    24         <!--        右侧小标题颜色-->
    25         <attr name="custom_setting_view_right_title_text_color" format="color" />
    26         <!--        右侧图片资源-->
    27         <attr name="custom_setting_view_right_src" format="reference" />
    28         <!--        右侧图片资源是否显示-->
    29         <attr name="custom_setting_view_right_visibility" format="boolean" />
    30     </declare-styleable>
    自定义属性
  • 相关阅读:
    gulp编译sass
    Material Design学习
    js爬虫心得
    js爬虫
    angularjs
    sass心得
    矩阵与线性代数学习笔记
    2-SAT学习笔记
    一个有趣的题目【二分答案,2-SAT,线段树优化】
    vijos训练之——星辰大海中闪烁的趣题
  • 原文地址:https://www.cnblogs.com/CoderSilence/p/12967248.html
Copyright © 2020-2023  润新知