当方向发生默认标题时如何保持标题不改变它android
how to keep title not changing it when orientation happen to default title android
我创建了 activity 通过单击导航抽屉中的菜单项打开了多个片段,我面临的问题是当我 select 某些项目和相关片段在工具栏中以正确的标题打开时然后旋转应用程序片段停留在它是(这是我想要的和我所做的,我使用 savedInstanceState 来保留它)工具栏中的标题更改为默认标题!
我不知道如何将标题与片段保持一致
这里是 activity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
TextView header;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
header = findViewById(R.id.header);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setupDrawer(toolbar);
if(savedInstanceState==null) {
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content, fragment);
fragmentTransaction.commit();
header.setText("Home");
}
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setTitle(null);
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeAsUpIndicator(R.drawable.ic_launcher_foreground);
}
}
private void setupDrawer(Toolbar toolbar) {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.nav_home:
fragment = new HomeFragment();
header.setText("Home");
break;
case R.id.nav_settings:
fragment = new SettingsFragment();
header.setText("Settings");
break;
case R.id.nav_about:
fragment = new AboutFragment();
header.setText("About app");
break;
}
if (fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content, fragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
这里 xml 代表 activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<include layout="@layout/toolbar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextColor="@color/colorPrimary"
app:itemIconTint="@color/colorPrimary"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/navigation_drawer">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
我的工具栏 xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
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"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/holo_red_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="28sp"
android:text="Home"
android:textAllCaps="true"
android:textColor="@android:color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
我搜索并找到了一些解决方案,但对我没有用,或者我不知道如何正确地做,所以请帮忙,在此先感谢
1-
尝试在 xml 布局中的文本中声明 saveEnabled = "true"。
2- 试试这个:
header = findViewById(R.id.header);
if (myNewBoolean) {
tvNowUsedFiltres.setVisibility(View.VISIBLE);
}
3- 也试试这个:
将其放入您的片段 onCreate()
setRetainInstance(boolean)
我找到了一个解决方案,实际上我之前尝试过但是我做错了,现在我重新做了我所做的..我找到的解决方案是使用以下代码:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(STATE_SELECTED_KEY, title);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
title = savedInstanceState.getString(STATE_SELECTED_KEY, "Home");
header.setText(title);
}
变量标题的值在选定的菜单项上发生变化,然后我们只需将其保存在 onSaveInstanceState(Bundle outState) 上并调用 onRestoreInstanceState(Bundle savedInstanceState) 将其取回
我创建了 activity 通过单击导航抽屉中的菜单项打开了多个片段,我面临的问题是当我 select 某些项目和相关片段在工具栏中以正确的标题打开时然后旋转应用程序片段停留在它是(这是我想要的和我所做的,我使用 savedInstanceState 来保留它)工具栏中的标题更改为默认标题!
我不知道如何将标题与片段保持一致
这里是 activity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
TextView header;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
header = findViewById(R.id.header);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setupDrawer(toolbar);
if(savedInstanceState==null) {
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content, fragment);
fragmentTransaction.commit();
header.setText("Home");
}
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setTitle(null);
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeAsUpIndicator(R.drawable.ic_launcher_foreground);
}
}
private void setupDrawer(Toolbar toolbar) {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.nav_home:
fragment = new HomeFragment();
header.setText("Home");
break;
case R.id.nav_settings:
fragment = new SettingsFragment();
header.setText("Settings");
break;
case R.id.nav_about:
fragment = new AboutFragment();
header.setText("About app");
break;
}
if (fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content, fragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
这里 xml 代表 activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<include layout="@layout/toolbar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextColor="@color/colorPrimary"
app:itemIconTint="@color/colorPrimary"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/navigation_drawer">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
我的工具栏 xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
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"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/holo_red_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="28sp"
android:text="Home"
android:textAllCaps="true"
android:textColor="@android:color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
我搜索并找到了一些解决方案,但对我没有用,或者我不知道如何正确地做,所以请帮忙,在此先感谢
1- 尝试在 xml 布局中的文本中声明 saveEnabled = "true"。
2- 试试这个:
header = findViewById(R.id.header);
if (myNewBoolean) {
tvNowUsedFiltres.setVisibility(View.VISIBLE);
}
3- 也试试这个:
将其放入您的片段 onCreate()
setRetainInstance(boolean)
我找到了一个解决方案,实际上我之前尝试过但是我做错了,现在我重新做了我所做的..我找到的解决方案是使用以下代码:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(STATE_SELECTED_KEY, title);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
title = savedInstanceState.getString(STATE_SELECTED_KEY, "Home");
header.setText(title);
}
变量标题的值在选定的菜单项上发生变化,然后我们只需将其保存在 onSaveInstanceState(Bundle outState) 上并调用 onRestoreInstanceState(Bundle savedInstanceState) 将其取回