首页 > 解决方案 > 如何制作一个看起来像这样的首选项屏幕?

问题描述

设置页面

我在哪里可以得到这种风格的偏好屏幕?我对围绕属于一个偏好类别的每个偏好列表创建圆角矩形特别感兴趣。我不确定这是否可以使用android:layout带有偏好的标签?

标签: androidpreferences

解决方案


您可以在 Drawable 目录中创建一个custom_background.xml,如下所示:

右键单击可绘制目录->新建->可绘制资源文件->输入文件名(这里是 custom_background)并在根元素中选择/输入“形状”

现在在你的custom_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#2196f3" />
    <corners android:radius="10dp" />
</shape>

您还可以在custom_background.xml .

现在对于每个首选项,您可以在主布局下有一个单独的 ConstraintLayout(或您正在使用的任何其他布局),然后按如下方式提供该首选项布局custom_background.xmlbackground属性:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="4dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/your_preference_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@drawable/custom_background">
        
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

main_layout根据您想要的外观进行填充。
让我知道这是否能解决您的问题。


推荐阅读