本文介绍了PreferenceFragment重叠工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
作为标题,我的PreferenceFragment与我的工具栏重叠.我已经尝试过其他解决方案,但是问题仍然存在.希望对您有所帮助.这是我的代码.
As the title, my PreferenceFragment overlaps my toolbar. I've already tryed different solutions but the problem persists. Hopes in any help. Here is my code.
活动:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
// Display the fragment as the main content
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
}
活动XML(activity_settings.xml):
Activity XML (activity_settings.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mypackage.MyActivity">
<include
layout="@layout/app_bar" />
<FrameLayout
android:id="@+id/pref_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/app_bar"/>
</RelativeLayout>
片段(SettingsFragment.java):
Fragment (SettingsFragment.java):
import android.preference.PreferenceFragment;
import android.os.Bundle;
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_settings);
}
}
片段XML(pref_settings.xml):
Fragment XML (pref_settings.xml):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="pref_checkbox"
android:title="Title"
android:summary="Summary"
android:defaultValue="false"/>
</PreferenceScreen>
工具栏(app_bar.xml):
Toolbar (app_bar.xml):
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/app_bar"
android:background="@color/primaryColor"
android:elevation="3dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark" />
推荐答案
更改此内容:
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
收件人:
getFragmentManager().beginTransaction().replace(R.id.pref_content, new SettingsFragment()).commit();
这篇关于PreferenceFragment重叠工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!