我在onCreate中生成SettingsFragment,然后根据用户输入更改内容布局。但是在用户选择返回后,我将setContentLayout设置为第一个,但SettingsFragment分配器为。我的意思是这就像将能见度设置为GONE。 我希望它看起来正常。
我尝试过的方法: 1.将此getSupportFragmentManager()...放入if(savedInstanceState == null)块中。 2.复制getSupportFragmentManaga ...,然后将其粘贴到goToSettingsRoot(View view){..}
方法中我有包含以下内容的SettingsActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
.
.
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
.
.
}
.
.
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
.
.
public void openProfileSettings(View view) {
setContentView(R.layout.user_details);
.
.
}
public void goToSettingsRoot(View view) {
setContentView(R.layout.settings_activity);
}
settings_activity.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/settings_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="openProfileSettings"
android:background="?attr/selectableItemBackground">
<ImageView
android:id="@+id/imageView"
android:layout_width="40sp"
android:layout_height="40sp"
android:src="@drawable/ic_profile_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<FrameLayout
android:id="@+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
root_preferences.xml:
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
<PreferenceCategory
app:icon="@drawable/ic_settings_dark"
app:title="Settings">
<SwitchPreferenceCompat
app:key="sync"
app:summaryOff="q"
app:summaryOn="w"
app:title="e" />
<SwitchPreferenceCompat
app:dependency="sync"
app:key="attachment"
app:summaryOff="z"
app:summaryOn="y"
app:title="x" />
</PreferenceCategory>
</PreferenceScreen>
任何帮助将不胜感激。