gpt4 book ai didi

android - 如何强制子 PreferenceScreen 包含在 Fragment 布局中 (Android)

转载 作者:搜寻专家 更新时间:2023-11-01 09:48:06 26 4
gpt4 key购买 nike

我对 Android 的首选项系统还很陌生,目前遇到了一个问题。

根据 Android 指南 (http://developer.android.com/guide/topics/ui/settings.html) 的建议,我使用 Preference Fragment。因此,我的 SettingsActivity 包含一些内容(标题、标签等标题)和下面的 PreferenceFragment。

问题是,当我点击与“子”PreferenceScreen 关联的首选项时,“新首选项屏幕”不遵循我的 fragment 布局,而是填充整个 Activity 。

这是一个示例:假设我有一个调用 addPreferenceFromResource(R.xml.preferences) 的 PreferenceFragment。 preferences.xml 确实包含“更改密码”首选项,这是一个包含 3 个 TextEditPreference 的 PreferenceScreen。

首选项.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">   
<EditTextPreference android:title="@string/pref_one"/>
<EditTextPreference android:title="@string/pref_two"/>

<PreferenceScreen android:title="@string/pref_change_password">

<EditTextPreference android:title="@string/pref_current_pass"
android:inputType="textPassword"
android:hint="@string/hint_current_password" />

<EditTextPreference android:title="@string/pref_new_pass"
android:inputType="textPassword"
android:hint="@string/hint_new_password" />

<EditTextPreference android:title="@string/pref_confirm_new_pass"
android:inputType="textPassword"
android:hint="@string/hint_confirm_new_password" />

</PreferenceScreen>
</PreferenceScreen>

因此,当我单击 PreferenceScreen 时,它会执行以下操作: The entire activity is filled by the preferenceScreen

但我希望它这样做: enter image description here

我该怎么做?预先感谢您的回答!

最佳答案

好吧,在深入研究 Android 引用之后,我找到了解决方案。

看起来,当单击子 PreferenceScreen 时,会打开包含新 Preference 对象的 Dialog,而不是新 Activity 或其他任何内容。

因此解决方案是检索对话框并使其适合原始 PreferenceFragment 的布局。为此,我使用了 onPreferenceTreeClick 回调方法来检测 PreferenceScreen 是否被点击:

public class SettingsFragment extends PreferenceFragment {

// onCreate and other stuff...

@Override
public boolean onPreferenceTreeClick (PreferenceScreen preferenceScreen,
Preference preference) {

// Initiating Dialog's layout when any sub PreferenceScreen clicked
if(preference.getClass() == PreferenceScreen.class) {
// Retrieving the opened Dialog
Dialog dialog = ((PreferenceScreen) preference).getDialog();
if(dialog == null) return false;

initDialogLayout(dialog); // Initiate the dialog's layout
}
return true;
}

private void initDialogLayout(Dialog dialog) {
View fragmentView = getView();

// Get absolute coordinates of the PreferenceFragment
int fragmentViewLocation [] = new int[2];
fragmentView.getLocationOnScreen(fragmentViewLocation);

// Set new dimension and position attributes of the dialog
WindowManager.LayoutParams wlp = dialog.getWindow().getAttributes();
wlp.x = fragmentViewLocation[0]; // 0 for x
wlp.y = fragmentViewLocation[1]; // 1 for y
wlp.width = fragmentView.getWidth();
wlp.height = fragmentView.getHeight();

dialog.getWindow().setAttributes(wlp);

// Set flag so that you can still interact with objects outside the dialog
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
}
}

就是这样,成功了。如果您认为有更好的方法,请发表评论。

关于android - 如何强制子 PreferenceScreen 包含在 Fragment 布局中 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36941512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com