当前位置:   article > 正文

自定义类似微信效果Preference

自定义类似微信效果Preference

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

  1. <?xml version="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. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="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. <androidx.appcompat.widget.AppCompatTextView
  11. android:id="@+id/custom_preference_text"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:textSize="16sp"
  15. android:textColor="#111111"
  16. android:text="@string/text_help_page"
  17. android:layout_marginStart="20dp"
  18. app:layout_constraintStart_toStartOf="parent"
  19. app:layout_constraintTop_toTopOf="parent"
  20. app:layout_constraintBottom_toBottomOf="parent"/>
  21. <androidx.appcompat.widget.AppCompatImageView
  22. android:id="@+id/custom_preference_icon"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:src="@drawable/ic_preference_back"
  26. android:layout_marginEnd="20dp"
  27. app:layout_constraintEnd_toEndOf="parent"
  28. app:layout_constraintTop_toTopOf="parent"
  29. app:layout_constraintBottom_toBottomOf="parent"/>
  30. </androidx.constraintlayout.widget.ConstraintLayout>

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. 效果如图:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/440161
推荐阅读
相关标签
  

闽ICP备14008679号