首选项片段作为活动的一部分

祖法尔·穆哈马德耶夫

我需要为首选项创建活动并在底部(在首选项下)放置一些视图。我创建了布局,但只有第一类标签可见。为什么开关是隐形的?这是我所拥有的:

  • 活动布局.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
        <ScrollView
            android:id="@+id/settingsForm"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <fragment
                    android:id="@+id/settingsFragment"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    class="com.example.SettingsFragment"/>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <!-- views -->
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
    
  • SettingsFragment 扩展 PreferenceFragment:

            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.preferences);
                //...
            }
    
  • 首选项.xml

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory
            android:title="@string/pref_work_storage_title"
            android:key="pref_work_category">
            <SwitchPreference
                android:key="pref_work_unavailable"
                android:defaultValue="true"
                android:summaryOn="@string/pref_work_available"
                android:summaryOff="@string/pref_work_unavailable"/>
        </PreferenceCategory>
    </PreferenceScreen>        
    
祖法尔·穆哈马德耶夫

我发现问题。首选项片段本身在可滚动列表中,因此为了实现所需的行为,我实现了自定义首选项。这是自定义首选项布局的示例(无首选项扩展)。

我还需要添加点击侦听器。为了实现它,我扩展了Preference类并在onCreateView方法中添加了侦听器这是扩展偏好的示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章