gpt4 book ai didi

android - PreferenceFragment 重叠工具栏

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:23:18 25 4
gpt4 key购买 nike

如标题所示,我的 PreferenceFragment 与我的工具栏重叠。我已经尝试过不同的解决方案,但问题仍然存在。希望有任何帮助。这是我的代码。

Activity :

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();
}
}

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>

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);
}

}

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):

<?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();

关于android - PreferenceFragment 重叠工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35682526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com