自定义首选项Android Kotlin [英] Custom Preference Android Kotlin

查看:173
本文介绍了自定义首选项Android Kotlin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对Preference进行子类化,以在Kotlin中创建自定义首选项.我无法在首选项"屏幕中获取自定义首选项以进行充气.如果我从首选项"屏幕中删除了此自定义首选项,则我已实现的其余首选项(此处未显示)都可以正常工作. 这里有许多类似的 seeming 问题,但我发现的这些问题都没有直接涉及创建自定义首选项的Kotlin实现的问题.

I'd like to subclass Preference to create a custom preference item in Kotlin. I am unable to get the custom preference to inflate in the Preference screen. If I remove this custom preference from my preference screen, the rest of the preferences I have implemented (not shown here) are working fine. There are many similar seeming questions here, but none of the ones I've found directly deal with the issue of creating a Kotlin implementation of a custom preference.

请帮我提供一个经过测试的有效示例 ,该示例显示了三件事:

Please help me with a working example that you've tested that shows three things:

  1. custom_preference.xml
  2. CustomPreference.kt
  3. preference_screen.xml(父母偏好设置"屏幕,用于显示自定义偏好设置)
  1. custom_preference.xml
  2. CustomPreference.kt
  3. preference_screen.xml (Parent Preference screen to display custom preference)

这是我的代码: 一个自定义的xml首选项项,它显示一个字符串(尽管我的首选项最终会具有更多的功能,但是使该示例保持简单)

Here is my code: A custom xml preference item that displays a string (let's keep it simple for the example, although my preference will end up having significantly more functionality)

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并包含适当构造函数的类.

A class that extends Preference and includes the proper constructors.

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中的自定义首选项声明.

The custom preference declaration in the PreferenceScreen.

preference_screen.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.

Note: I manually renamed class names here for the example. Also, I must use the support libs instead of Androidx for this project.

推荐答案

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

Try to set your layout resource in initialization of class, instead of onBindViewHolder. Also you should create the layout using normal widget elements (not 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>

preference_screen.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 Kotlin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆