我想从设置为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
答案 0 :(得分:1)
在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,以检查布局是否正确...
希望对您有所帮助!
答案 1 :(得分:0)
您可以使用onCreatePreferences
在setPreferencesFromResource
中添加首选项布局,以获取更多详细信息,请访问PreferenceFragmentCompat和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);
}
}
}