现在是完整的答案:
将以下行添加到您的App-Gradle中:implementation 'androidx.preference:preference:1.1.1'
或者针对Kotlin implementation 'androidx.preference:preference-ktx:1.1.1'
。然后同步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) {
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>
并且在您的Activity Code中,在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)>);
<YourSettingsFragmentClass> settingsFragment = new <YourSettingsFragmentClass>();
settingsFragment. ...
getSupportFragmentManager().beginTransaction().replace(R.id.<YourFrameLayout>,settingsFragment).commit();
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>
就是这样,玩得愉快。