• 自定义类似微信效果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. 效果如图:

  • 相关阅读:
    Dubbo学习(四)——Dubbo的常用场景
    分享一个2022年火遍全网的Python框架
    centos7磁盘挂载及目录扩容
    人工智能科学计算库—Numpy教程
    C++中迭代器的使用
    [附源码]java毕业设计课程作业管理系统
    【Linux】基础IO —— 下(实现动静态库)
    【校招VIP】【约起来】高校大学生自己的商业项目|产品脑图的重要性:活动模型的细节
    Python:基础&爬虫
    MySQL的存储引擎
  • 原文地址:https://blog.csdn.net/dengfuma/article/details/137811567