android - 自定义偏好 Android Kotlin

标签 android kotlin android-recyclerview android-preferences preferencescreen

我想继承 Preference在 Kotlin 中创建自定义首选项。我无法在“首选项”屏幕中获取自定义首选项。如果我从我的首选项屏幕中删除此自定义首选项,我已实现的其余首选项(此处未显示)工作正常。 这里有许多类似的问题,但我发现没有一个直接涉及创建自定义首选项的 Kotlin 实现的问题。

请帮我提供一个工作示例你测试过的这显示了三件事:

  • custom_preference.xml
  • CustomPreference.kt
  • preference_screen.xml (显示自定义偏好的父偏好屏幕)

  • 这是我的代码:
    定制xml显示字符串的首选项(让我们在示例中保持简单,尽管我的首选项最终将具有更多功能)

    custom_preference.xml
    <Preference 
        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="@android:id/widget_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".CustomPreference">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="This is a custom preference" />
    </Preference>
    

    扩展 Preference 的类并包括适当的构造函数。

    CustomPreference.kt
    package com.example.myApp
    
    import android.content.Context
    import android.support.v7.preference.Preference
    import android.support.v7.preference.PreferenceViewHolder
    import android.util.AttributeSet
    import com.example.myApp.R
    import com.example.myApp.R.layout.custom_preference
    
    class CustomPreference (context: Context,
                                attrs: AttributeSet? = null,
                                defStyleAttr: Int = R.attr.preferenceStyle,
                                defStyleRes: Int = defStyleAttr)
        : Preference(context, attrs, defStyleAttr, defStyleRes) {
        override fun onBindViewHolder(holder: PreferenceViewHolder?) {
            super.onBindViewHolder(holder)
            layoutResource = custom_preference
        }
    }
    
    PreferenceScreen 中的自定义首选项声明.

    首选项屏幕.xml :
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <com.example.CustomPreference
            app:key="custom_preference_key"
            app:title="This is a custom preference" />
    </android.support.v7.preference.PreferenceScreen>
    

    注意:我在这里手动重命名了类名作为示例。还有,我必须在这个项目中使用支持库而不是 Androidx。

    最佳答案

    尝试在类的初始化中设置布局资源,而不是 onBindViewHolder .此外,您应该使用普通的小部件元素(不是 Preference )创建布局。

    CustomPreference.kt

    import android.content.Context
    import android.support.v7.preference.Preference
    import android.support.v7.preference.PreferenceViewHolder
    import android.util.AttributeSet
    import kotlinx.android.synthetic.main.custom_preference_layout.view.*
    
    class CustomPreference @JvmOverloads constructor(
            context: Context,
            attrs: AttributeSet,
            defStyleAttr: Int = 0
    ) : Preference(context, attrs, defStyleAttr) {
    
        init {
            widgetLayoutResource = R.layout.custom_preference_layout
        }
    
        override fun onBindViewHolder(holder: PreferenceViewHolder) {
            super.onBindViewHolder(holder)
            with(holder.itemView) {
                // do the view initialization here...
    
                textView.text = "Another Text"
            }
        }
    
    } 
    

    res/layout/custom_preference_layout.xml
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="This is a custom preference" />
    
    </FrameLayout>
    

    首选项屏幕.xml
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.preference.PreferenceScreen 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <com.example.CustomPreference
            app:key="custom_preference_key"
            app:title="This is a custom preference" />
    
    </android.support.v7.preference.PreferenceScreen>
    

    关于android - 自定义偏好 Android Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53834600/

    相关文章:

    Android 只有在 RecycleView/List 大于屏幕时才设置 StackFromEnd

    android - 取消所有接近警报

    android - 在 Android 编辑文本字段中显示一条消息

    java - 基于正则表达式的链接未按预期工作

    android - 如何使用 Eclipse + Proguard "run"导出的 Android 应用程序?

    android - 滚动 AppBarLayout 内部的 RecyclerView 在滚动外部 RecycleView 时禁用滚动滞后

    android - java.lang.IllegalStateException : Function not found androidx. compose.internal.restartableFunctionInstance 错误

    android - 为什么在 Android Studio 中构建 kotlin 模块的编译阶段 tmp\kotlin-classes 文件夹为空且找不到路径?

    android - 如何在android中使用数据绑定(bind)实现Switch

    android - Recyclerview 网络的 ImageView 中的相同图像