Android 从屏幕中删除/隐藏首选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2240326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove/hide a preference from the screen
提问by Alex Volovoy
I have an activity which extends PreferenceActivity. I'm loading preferences from the xml file. But in some cases i need completely hide one of the preferences from the screen based on my app state. There is a setEnabled method, but it's not exactly what i want. I want to remove that preference from the screen completely. Is it possible ?
我有一个扩展 PreferenceActivity 的活动。我正在从 xml 文件加载首选项。但在某些情况下,我需要根据我的应用程序状态从屏幕中完全隐藏其中一项首选项。有一个 setEnabled 方法,但这不是我想要的。我想从屏幕上完全删除该首选项。是否可以 ?
回答by Kavi
If your Preference
is within a PreferenceCategory
, you have to do this:
如果您Preference
在 a 内PreferenceCategory
,则必须执行以下操作:
XML:
XML:
<PreferenceCategory
android:key="category_foo"
android:title="foo">
<CheckBoxPreference
android:key="checkPref" />
Java:
爪哇:
CheckBoxPreference mCheckBoxPref = (CheckBoxPreference) findPreference("checkPref");
PreferenceCategory mCategory = (PreferenceCategory) findPreference("category_foo");
mCategory.removePreference(mCheckBoxPref);
回答by dxh
Yes, if you have a reference to both the Preference
, and its parent (a PreferenceCategory
, or PreferenceScreen
)
是的,如果您同时引用Preference
, 及其父级 (a PreferenceCategory
, or PreferenceScreen
)
myPreferenceScreen.removePreference(myPreference);
回答by 1''
In the case where the Preference is a direct child of the preference screen, here is some stand-alone code:
如果 Preference 是首选项屏幕的直接子级,这里是一些独立代码:
PreferenceScreen screen = getPreferenceScreen();
Preference pref = getPreferenceManager().findPreference("mypreference");
screen.removePreference(pref);
回答by James
If you want something that will dynamically change the prefs for example on a SwitchPreference, I have found the best way is to put all my sub options into two category containers. Initially you'll have everything shown, then you just remove the bits you don't want. The clever bit, is you just trigger recreate when something changes and then you don't have to manually create anything or worry about putting things back in in the correct order.
如果您想要一些可以动态更改首选项的东西,例如在 SwitchPreference 上,我发现最好的方法是将所有子选项放入两个类别容器中。最初您将显示所有内容,然后您只需删除您不想要的部分。聪明的一点是,您只需在发生变化时触发重新创建,然后您就不必手动创建任何内容或担心将事物按正确的顺序放回原处。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
PreferenceCategory prefCatOne= (PreferenceCategory)findPreference("prefCatOne");
PreferenceCategory prefCatTwo= (PreferenceCategory)findPreference("prefCatTwo");
SwitchPreference mySwitchPref= (SwitchPreference)findPreference("mySwitchPref");
PreferenceScreen screen = getPreferenceScreen();
if (mySwitchPref.isChecked()) {
screen.removePreference(prefCatOne);
} else {
screen.removePreference(prefCatTwo);
}
}
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (key.equals("mySwitchPref")) {
this.recreate();
}
}
The only downside that I can see with this, is there is a flash as the screen is recreated from scratch.
我可以看到的唯一缺点是,当屏幕是从头开始重新创建时,会出现闪光。
回答by user3165739
In your XML file:
在您的 XML 文件中:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="preferenceScreen">
<PreferenceCategory
android:key="personalisation"
android:title="your title here">
<ThemedPreference
android:key="animation" />
</PreferenceScreen>
In your code:
在您的代码中:
PreferenceScreen pPreferenceScreen = (PreferenceScreen) findPreference("preferenceScreen");
PreferenceCategory pCategory = (PreferenceCategory) findPreference("personalisation");
ThemedPreference pThemePref = (ThemedPreference) findPreference("animation");
pPreferenceScreen.removePreference(pCategory); //remove category
pCategory.removePreference(pThemePref); // remove preference
回答by aaronvargas
If you are using PreferenceFragmentCompatyou can set the visiblity in xml.
如果您使用PreferenceFragmentCompat,您可以在 xml 中设置可见性。
The preferences in your xml will be converted to AppCompat versions automatically. You can then use the 'app:isPreferenceVisible' attribute in your xml
您的 xml 中的首选项将自动转换为 AppCompat 版本。然后,您可以在 xml 中使用“ app:isPreferenceVisible”属性
preferences.xml
首选项.xml
<PreferenceScreen 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">
<CheckBoxPreference
android:defaultValue="false"
android:key="show.navigation"
android:title="Show navigation"
app:isPreferenceVisible="false" />
...
The attribute is documented at https://developer.android.com/guide/topics/ui/settings/components-and-attributes
该属性记录在https://developer.android.com/guide/topics/ui/settings/components-and-attributes
Adding PreferenceFragmentCompat
is documented at https://developer.android.com/guide/topics/ui/settings/#inflate_the_hierarchy
添加PreferenceFragmentCompat
记录在https://developer.android.com/guide/topics/ui/settings/#inflate_the_hierarchy
Example:
例子:
public class MySettingsActivity extends AppCompatActivity {
public static class MySettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_container, new MySettingsFragment())
.commit();
}
}
回答by Tea
I recommend using v7 preference, it has setVisible()
method. But I have not tried it yet. Accordingly you have to use PreferenceFragment
instead of PreferenceActivity
.
我推荐使用 v7 偏好,它有setVisible()
方法。但我还没有尝试过。因此,您必须使用PreferenceFragment
代替PreferenceActivity
.
回答by Al Ro
In the XML file you can make a hidden preference by leaving the title and summary tags empty.
在 XML 文件中,您可以通过将标题和摘要标签留空来进行隐藏首选项。
<EditTextPreference
android:defaultValue="toddlerCam"
android:key="save_photo_dir"
/>
回答by Sam
Here's a generic way to do this that works regardless of whether the preference is under a PreferenceCategory
or PreferenceScreen
.
这是执行此操作的通用方法,无论首选项是否在 aPreferenceCategory
或PreferenceScreen
.
private void removePreference(Preference preference) {
PreferenceGroup parent = getParent(getPreferenceScreen(), preference);
if (parent == null)
throw new RuntimeException("Couldn't find preference");
parent.removePreference(preference);
}
private PreferenceGroup getParent(PreferenceGroup groupToSearchIn, Preference preference) {
for (int i = 0; i < groupToSearchIn.getPreferenceCount(); ++i) {
Preference child = groupToSearchIn.getPreference(i);
if (child == preference)
return groupToSearchIn;
if (child instanceof PreferenceGroup) {
PreferenceGroup childGroup = (PreferenceGroup)child;
PreferenceGroup result = getParent(childGroup, preference);
if (result != null)
return result;
}
}
return null;
}
回答by birdy
Since Android API 26 getParent()
method is available: https://developer.android.com/reference/android/preference/Preference.html#getParent()
由于 Android API 26getParent()
方法可用:https: //developer.android.com/reference/android/preference/Preference.html#getParent()
Though you can do the following:
虽然您可以执行以下操作:
preference.getParent().removePreference(preference);