Android 如何创建设置活动

Android 如何创建设置活动,android,android-fragments,android-preferences,Android,Android Fragments,Android Preferences,我在我的应用程序中创建了一个设置活动,但它不起作用。 这是我的SettingFragment类: public class SettingFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { setPreferencesFromResource(R.x

我在我的应用程序中创建了一个设置活动,但它不起作用。 这是我的
SettingFragment
类:

public class SettingFragment extends PreferenceFragmentCompat
{
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey)
    {
        setPreferencesFromResource(R.xml.pref, rootKey);
    }
}
这是我的
设置活动

public class SettingActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.content, new SettingFragment())
                .commit();
    }
}
这是
main活动
。单击按钮将打开
设置活动

btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent intent = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intent);
            }
        });
这是
Preference.xml

<PreferenceScreen 
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Preference
        app:key="feedback"
        app:title="Send feedback"
        app:summary="Report technical issues or suggest new features"/>

</PreferenceScreen>

什么是
R.id.content
?在站点示例中使用了它。
有什么问题?如何解决这个问题?

您需要使用
android.R.id.content
。这将用片段的内容替换根内容。您可以从中找到有关android.R.id.content的更多信息

//将片段显示为主要内容。
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content,新设置片段())
.commit();

您需要使用
android.R.id.content
。这将用片段的内容替换根内容。您可以从中找到有关android.R.id.content的更多信息

//将片段显示为主要内容。
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content,新设置片段())
.commit();
R.id的内容是什么

假设这是
设置活动
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingsActivity">

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp" >
    </LinearLayout>

</LinearLayout>

调用
replace
将用
R.id.content
交换片段。您还可以调用replace with
android.R.id.content
,了解更多信息

R.id的内容是什么

假设这是
设置活动
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingsActivity">

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp" >
    </LinearLayout>

</LinearLayout>


调用
replace
将用
R.id.content
交换片段。您还可以调用replace with
android.R.id.content
,阅读更多信息。

我们需要的错误比“它不起作用”更大一点,以确定您试图解决的问题。你犯了什么错误?要回答您关于
R
的问题,它是一个自动生成的文件,包含所有资源ID。什么不起作用?你犯了什么样的错误?还包括
activity\u setting.xml
layout文件。我们需要一个比“它不起作用”更大的错误来确定您试图解决的问题。你犯了什么错误?要回答您关于
R
的问题,它是一个自动生成的文件,包含所有资源ID。什么不起作用?你犯了什么样的错误?还包括
activity\u setting.xml
layout文件。