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

时间:2017-11-02 10:44:28

标签: android android-fragments

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

  • activity_layout.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);
                //...
            }
    
  • 的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>        
    

1 个答案:

答案 0 :(得分:0)

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

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

相关问题