如何从PreferenceFragmentCompat访问自定义布局?

问题描述 投票:0回答:2

我想从设置为preference的自定义布局访问视图>

settings_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">

    <Preference
            android:key="storage"
            android:layout="@layout/layout_storage" />

    ...
</PreferenceScreen>

SettingsFragment.kt

class SettingsFragment: PreferenceFragmentCompat() {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        addPreferencesFromResource(R.xml.settings_screen)
    }
}

关于stackoverflow的问题与此类似,但是在该答案中,他正在从主布局文件而不是从preference访问视图

PreferenceFragmentCompat custom layout

而且我也找到了这篇文章,他使用onBindView方法访问自定义视图。

https://code.luasoftware.com/tutorials/android/override-layout-of-android-preference/

但是onBindView中没有方法调用PreferenceFragmentCompat

更新

layout_storage.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:padding="20dp"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
            android:paddingBottom="5dp"
            android:orientation="horizontal"
            android:layout_width="match_parent" android:layout_height="wrap_content">

        <TextView
                android:textColor="@color/colorGray"
                android:paddingTop="5dp"
                android:layout_weight="1"
                android:text="USED"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>

        <TextView
                android:textColor="@color/colorGray"
                android:text="FREE"
                android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    </LinearLayout>


    <ProgressBar
            android:progress="30"
            android:scaleY="5"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:id="@+id/progressBar"/>

</LinearLayout>

现在我想获取progressBar

我想从设置为首选项settings_screen.xml的自定义布局访问视图

android android-preferences
2个回答
0
投票

您可以使用onCreatePreferencessetPreferencesFromResource中添加您的首选项布局以获取更多详细信息,请访问PreferenceFragmentCompatAndroid App Settings with Preference Fragment Compat


0
投票

重写PreferenceFragmentCompat中的以下方法:

© www.soinside.com 2019 - 2024. All rights reserved.