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

How do access custom layout from PreferenceFragmentCompat?

我想从设置为 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)
    }
}

在 Whosebug 上有一个类似的问题,但在那个答案中,他是从主布局文件而不是 preference

访问视图

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

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

但是PreferenceFragmentCompat

中没有方法调用onBindView

已更新

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

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

public static class SettingsFragment extends PreferenceFragmentCompat {

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    // Load the preferences from an XML resource
    setPreferencesFromResource(R.xml.settings_screen, rootKey);

    SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();

    PreferenceScreen preferenceScreen = getPreferenceScreen();

    int count = preferenceScreen.getPreferenceCount();

    for (int i = 0; i < count ; i++) {
    Preference p = preferenceScreen.getPreference(i);
    if (!(p instanceof CheckBoxPreference)) {
    String value = sharedPreferences.getString(p.getKey(), "");
    setPreferenceSummery(p, value);
      }
     }
}
}

更改共享首选项时调用回调的接口定义- onSharedPreferenceChanged

    @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

  Preference preference = findPreference(key);

  if (preference != null) {

  if (!(preference instanceof CheckBoxPreference)) {
  String value = sharedPreferences.getString(preference.getKey(), "");
  setPreferenceSummery(preference, value);
        }
     }
  }

重写 PreferenceFragmentCompat 中的以下方法:

@Override
protected RecyclerView.Adapter onCreateAdapter(PreferenceScreen preferenceScreen) {
    return new PreferenceGroupAdapter(preferenceScreen) {
        public PreferenceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            PreferenceViewHolder holder = super.onCreateViewHolder(parent, viewType);
            View customLayout = holder.itemView;
            if (customLayout.getId() == R.id.myCustomId) {
                // get ref on your layout or modify it
            }
            return holder;
        }
    };
}

您可能想在 LinearLayout 中添加一个 id 以检查布局是否正确...
希望对您有所帮助!