现在完整的答案:
-
将此行添加到您的 App-Gradle:implementation 'androidx.preference:preference:1.1.1' 或 implementation 'androidx.preference:preference-ktx:1.1.1' 对于 Kotlin。并同步 Gradle。创建
res 文件夹中名为 xml 的目录。
-
在此目录中创建一个具有您喜欢的名称的 XML 文件,例如 main_preferences。根元素必须是
androidx.preference.PreferenceScreen.
-
用您的设置填充 XML 文件,例如:
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<androidx.preference.SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="Turn this option on or off"
android:title="Settings option" />
</androidx.preference.PreferenceScreen>
- 在文件夹 com.???.??? 的某处创建例如名为
MainSettingsFragment 的java 文件。超类(意味着 <Classname> extends <Superclass>)必须是 PreferenceFragmentCompat 并覆盖
onCreatePreferences。您可以复制此代码:
import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;
import com.???.???.R;
public class <YourClassname> extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// Load the preferences from an XML resource
setPreferencesFromResource(R.xml.<yourXmlFilename>, rootKey);
}
}
- 接下来,有两个选项可以在您的 .
中实现 PreferencesSreen
美丽和最好的方法是,它在创建时按代码实现它。
添加您的SettingsActivty 和FrameLayout 并给它一个ID,例如fl_main_settings:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/<!-- yourID -->">
</FrameLayout>
在您的活动代码中,在 onCreate 方法的顶部添加:
package com.???.???;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.MenuItem;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.text.HtmlCompat;
import com.???.???.MainSettingsFragment;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.<SettingsActivityXML(with the FrameLayout)>);
//If you want to insert data in your settings
<YourSettingsFragmentClass> settingsFragment = new <YourSettingsFragmentClass>();
settingsFragment. ...
getSupportFragmentManager().beginTransaction().replace(R.id.<YourFrameLayout>,settingsFragment).commit();
//Else
getSupportFragmentManager().beginTransaction().replace(R.id.<YourFrameLayout>,new <YourSettingsFragmentClass>()).commit();
}
或者您在 SettingsActivityXml 中实现 Fragment。 但我不建议这样做,因为启动活动需要几秒钟:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:baselineAligned="false">
<fragment
android:tag="frag"
android:name="com.quickme.musicme.Fragments.MainSettingsFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
就是这样,玩得开心。