下面是首选项屏幕的屏幕截图:
我还包括布局和java代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
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/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary_dark"
android:elevation="0dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
tools:ignore="UnusedAttribute" />
<?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">
<include layout="@layout/toolbar" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Notifications">
<SwitchPreference
android:defaultValue="false"
android:key="currencyNotificationOnOff"
android:title="@string/notification_enable" />
<com.adwitiya.currencyplus.view.TimePickerDialog
android:defaultValue="08:00"
android:dependency="currencyNotificationOnOff"
android:key="scheduleNotificationTime"
android:showDefault="true"
android:title="Notification Time" />
</PreferenceCategory>
<PreferenceCategory android:title="">
<Preference
android:key="about"
android:title="@string/about" />
<Preference
android:key="disclaimer"
android:title="@string/disclaimer" />
<Preference
android:key="help"
android:title="@string/help" />
</PreferenceCategory>
<PreferenceCategory android:title="General">
<Preference
android:key="spreadWord"
android:title="@string/prefs_spread_word" />
<Preference
android:key="rateUs"
android:title="@string/rate_us" />
<Preference
android:key="feedback"
android:title="@string/feedback" />
</PreferenceCategory>
</PreferenceScreen>
public class SettingsActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment()).commit();
TimePickerDialog timePicker = new TimePickerDialog(this);
PreferenceManager.setDefaultValues(this, R.xml.fragment_settings, false);
}
}
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_settings);
handleSettingsOptions();
}
....
}
我无法将工具栏保留在顶部并将首选项屏幕元素保留在其下方。滚动选项时,它会移动到工具栏顶部。
我使用单个首选项屏幕,每个首选项都会打开一个网络视图。
提前感谢您的回复。
希望这对其他人有用,接受的答案对你不起作用。
在 Layout/activity_setting.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">
<include layout="@layout/toolbar" />
<!-- added code -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</LinearLayout>
在 SettingsActivity.java 中,替换此代码
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment()).commit();
有了这个
getFragmentManager().beginTransaction()
.replace(R.id.container, new SettingsFragment()).commit();
解决了这个问题。我所做的更改如下:
以下对我有用:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
这样您仍然可以使用常规工具栏,并且其主题将保持不变。
将此添加到主要活动 xml...
android:fitsSystemWindows="true"
如果您不希望在主活动中出现这种行为,那么我发现的唯一方法是为首选项片段创建自定义工具栏 - Toolbar into PreferenceFragmentCompat。然后将此代码行添加到主题 xml 中的样式中...
<item name="android:fitsSystemWindows">true</item>