我有一个使用“自定义布局”的首选项屏幕:
android:layout="@layout/currencycodes"
这个问题是它在第一次尝试时无法调出布局。 正如您从下面的动画 gif 中看到的那样,我必须撤退并再次尝试第二次布局才能出现。为什么会这样?
preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="application_preferences">
<PreferenceCategory>
<PreferenceScreen
android:key="url_settings"
android:title="@string/url_settings"
android:summary="@string/summary_url_settings">
</PreferenceScreen>
<PreferenceScreen
android:key="currency_codes"
android:title="@string/left_handed"
android:summary="@string/summary_left_handed">
<Preference
android:key="currency_exchanges"
android:layout="@layout/currencycodes"/>
</PreferenceScreen>
<EditTextPreference
android:key="url_exchange_rate"
android:title="@string/url_exchange_rate_settings"
android:summary="@string/summary_url_exchange_rate"
android:enabled = "false"/>
</PreferenceCategory>
</PreferenceScreen>
接下来是使用的自定义布局。它包含一个 GridView。
currencycodes.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context=".MainActivity">
<GridView
android:layout_width="328dp"
android:layout_height="479dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent" android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="16dp"
android:id="@+id/grdExchanges" android:scrollbars="horizontal|vertical"
android:smoothScrollbar="true" tools:textAlignment="center"
android:verticalScrollbarPosition="defaultPosition"/>
</android.support.constraint.ConstraintLayout>
最佳答案
首先您需要了解 PreferenceScreen 的工作原理??
来自 [Android 文档][1]:
When it appears inside another preference hierarchy, it is shown and serves as the gateway to another screen of preferences (either by showing another screen of preferences as a Dialog or via a startActivity(android.content.Intent) from the getIntent()). The children of this PreferenceScreen are NOT shown in the screen that this PreferenceScreen is shown in. Instead, a separate screen will be shown when this preference is clicked.
简单来说,
首选项框架处理显示首选项层次结构中的这些其他屏幕。 此 PreferenceScreen 标记用作屏幕中断(类似于文字处理中的分页符)。与其他首选项类型一样,我们在此处分配一个键,以便它能够保存和恢复其实例状态。
在您的情况下,实际发生的是“application_preferences”将用作层次结构的根并提供给 PreferenceActivity。第一个屏幕将显示首选项“Url_Setting” >”和“货币代码”。 “Currency Codes”是“second_preferencescreen”,点击后会显示另一个偏好屏幕,即“Currency Exchange”(因为它偏好作为“second_preferencescreen”标签的子标签,即货币代码)。
所以,这就是为什么它在第一次点击时没有显示....但在第二次点击时希望你得到 首选项屏幕如何工作...
如果您还有任何疑问...请告诉我... https://developer.android.com/reference/android/preference/PreferenceScreen.html
关于android - 首选项屏幕自定义布局仅适用于第二次单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41708900/