为什么 settings.xml 布局与 ActionBar/Toolbar 重叠?
Why settings.xml layout is overlapping the ActionBar/Toolbar?
我正在开发 material 设计应用程序。
在 SettingsActivity.java
和 运行 应用程序中添加 settings.xml
后,activity 显示如下:
这是 SettingsActivity.java
文件的代码:
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
SpannableString s = new SpannableString("Settings");
s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(s);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
// do something
}
}
}
}
这是 activity_settings.xml
文件的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.xxx.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
</RelativeLayout>
这是 settings.xml
文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/pref_notification" >
<CheckBoxPreference
android:defaultValue="true"
android:key="prefNotification"
android:summary="@string/pref_notify_summary" >
</CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
我不知道为什么 settings.xml 布局与 ActionBar/Toolbar!
重叠
请告诉我。
提前致谢。
把你的activity_settings.xml改成这个
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.xxx.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_below="@id/toolbar"
android:layout_height="match_parent" />
</RelativeLayout>
并将代码更改为此
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new SettingsFragment())
.commit();
发现 Sunny 上面的解决方案中的 PreferenceActivity 可能会崩溃,因为它加载了一个没有 ListView 的内容视图。这是通过将它添加到 activity_settings.xml 来修复的,但是 PreferenceFragment 将忽略它。 activity_settings.xml 看起来像这样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/common_actionbar" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
当您调用 activity 上的片段时,不要使用
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
改为使用
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.Your_Frame_ID_On_xml, new SettingsFragment())
.commit();
我在使用 android.R.id.content
时遇到了同样的问题,因此将其更改为您用于框架的 ID,以将片段固定在 xml 上。
我正在开发 material 设计应用程序。
在 SettingsActivity.java
和 运行 应用程序中添加 settings.xml
后,activity 显示如下:
这是 SettingsActivity.java
文件的代码:
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
SpannableString s = new SpannableString("Settings");
s.setSpan(new TypefaceSpan(this, "Roboto-Medium.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(s);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String SHARE_APP_PREFERENCE_KEY = "pref_key_share_app";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settings);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(SHARE_APP_PREFERENCE_KEY)) {
// do something
}
}
}
}
这是 activity_settings.xml
文件的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.xxx.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
</RelativeLayout>
这是 settings.xml
文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/pref_notification" >
<CheckBoxPreference
android:defaultValue="true"
android:key="prefNotification"
android:summary="@string/pref_notify_summary" >
</CheckBoxPreference>
</PreferenceCategory>
</PreferenceScreen>
我不知道为什么 settings.xml 布局与 ActionBar/Toolbar!
重叠请告诉我。
提前致谢。
把你的activity_settings.xml改成这个
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abc.xxx.SettingsActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_below="@id/toolbar"
android:layout_height="match_parent" />
</RelativeLayout>
并将代码更改为此
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new SettingsFragment())
.commit();
发现 Sunny 上面的解决方案中的 PreferenceActivity 可能会崩溃,因为它加载了一个没有 ListView 的内容视图。这是通过将它添加到 activity_settings.xml 来修复的,但是 PreferenceFragment 将忽略它。 activity_settings.xml 看起来像这样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/common_actionbar" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
当您调用 activity 上的片段时,不要使用
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
改为使用
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.Your_Frame_ID_On_xml, new SettingsFragment())
.commit();
我在使用 android.R.id.content
时遇到了同样的问题,因此将其更改为您用于框架的 ID,以将片段固定在 xml 上。