• 自定义类似微信效果Preference


    1. 为自定义Preference 添加背景:custom_preference_background.xml

    1. "1.0" encoding="utf-8"?>
    2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
    3. <item>
    4. <shape android:shape="rectangle" >
    5. <gradient android:startColor="@color/white" android:endColor="@color/white" android:angle="90"/>
    6. <corners android:radius="8dp"/>
    7. shape>
    8. item>
    9. selector>

    2. 自定义layout: layout_custom_click_preference.xml

    1. "1.0" encoding="utf-8"?>
    2. "http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. android:layout_width="match_parent"
    5. android:layout_height="wrap_content"
    6. android:paddingVertical="20dp"
    7. android:layout_marginHorizontal="20dp"
    8. android:layout_marginVertical="8dp"
    9. android:background="@drawable/custom_preference_background">
    10. android:id="@+id/custom_preference_text"
    11. android:layout_width="wrap_content"
    12. android:layout_height="wrap_content"
    13. android:textSize="16sp"
    14. android:textColor="#111111"
    15. android:text="@string/text_help_page"
    16. android:layout_marginStart="20dp"
    17. app:layout_constraintStart_toStartOf="parent"
    18. app:layout_constraintTop_toTopOf="parent"
    19. app:layout_constraintBottom_toBottomOf="parent"/>
    20. android:id="@+id/custom_preference_icon"
    21. android:layout_width="wrap_content"
    22. android:layout_height="wrap_content"
    23. android:src="@drawable/ic_preference_back"
    24. android:layout_marginEnd="20dp"
    25. app:layout_constraintEnd_toEndOf="parent"
    26. app:layout_constraintTop_toTopOf="parent"
    27. app:layout_constraintBottom_toBottomOf="parent"/>

    3. 自定义style:

    1. <declare-styleable name="customClickPreferenceView">
    2. <attr name="iconDrawable" format="reference|color" />
    3. declare-styleable>

    4.自定义图标

    1. <vector xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:width="16dp"
    3. android:height="16dp"
    4. android:viewportWidth="16"
    5. android:viewportHeight="16">
    6. <path
    7. android:pathData="M5.869,12.862L5.869,12.862A0.667,0.667 45.145,0 1,5.869 11.919L10.319,7.47A0.667,0.667 45.145,0 1,11.262 7.47L11.262,7.47A0.667,0.667 45.145,0 1,11.262 8.413L6.812,12.862A0.667,0.667 45.145,0 1,5.869 12.862z"
    8. android:fillColor="#000"/>
    9. <path
    10. android:pathData="M5.682,2.729L5.682,2.729A0.667,0.667 45.145,0 1,6.625 2.729L11.075,7.178A0.667,0.667 45.145,0 1,11.075 8.121L11.075,8.121A0.667,0.667 45.145,0 1,10.132 8.121L5.682,3.672A0.667,0.667 45.145,0 1,5.682 2.729z"
    11. android:fillColor="#000000"/>
    12. vector>

    5. 完整代码

    1. public class CustomClickPreference extends Preference {
    2. private AppCompatTextView textView;
    3. private AppCompatImageView imageView;
    4. private int iconDrawable;
    5. public CustomClickPreference(Context context, AttributeSet attrs) {
    6. super(context, attrs);
    7. setLayoutResource(R.layout.layout_custom_click_preference);
    8. TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.customClickPreferenceView);
    9. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    10. iconDrawable = typedArray.getResourceId(R.styleable.customClickPreferenceView_iconDrawable, R.drawable.ic_preference_back);
    11. }
    12. typedArray.recycle();
    13. }
    14. @Override
    15. public void onBindViewHolder(PreferenceViewHolder holder) {
    16. super.onBindViewHolder(holder);
    17. // 自定义布局
    18. textView = (AppCompatTextView) holder.findViewById(R.id.custom_preference_text);
    19. imageView = (AppCompatImageView) holder.findViewById(R.id.custom_preference_icon);
    20. textView.setText(getTitle());
    21. imageView.setImageResource(iconDrawable);
    22. }
    23. public void setIconDrawable(int resourceId) {
    24. imageView.setImageResource(resourceId);
    25. }
    26. }

    6. 用法

    1. <com.tetras.sensechat.wedgit.CustomClickPreference
    2. app:iconDrawable="@drawable/ic_preference_back"
    3. app:isPreferenceVisible="false"
    4. app:key="key_document_help"
    5. app:title="帮助文档" />

    7. 效果如图:

  • 相关阅读:
    【算法挨揍日记】day06——1004. 最大连续1的个数 III、1658. 将 x 减到 0 的最小操作数
    hive笔记
    Android入门第20天-Android里的ScrollView的使用
    HackTheBox Photobomb 命令注入与远程代码执行和环境变量提权
    【操作系统】孤儿/僵尸/守护进程
    度量BGP监测源数量对AS可见性的影响
    【飞控调试】DJIF450机架+Pixhawk6c mini+v1.13.3固件+好盈Platinium 40A电调无人机调试
    仓库管理流程详解(附作业流程图)
    计算机网络面试知识点
    (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • 原文地址:https://blog.csdn.net/dengfuma/article/details/137811567