4

在我的應用中更改方向後,操作欄中的標題將從在導航器抽屜中選擇的片段的標題更改爲應用的標題。修復導航抽屜方向變化時的標題更改?

Navigation drawer example app保留它的標題與方向的變化,因爲它的片段是主要活動的一個子類,並擁有這行代碼

getActivity().setTitle(planet); 

現在我有點新手,所以我不知道如何保留標題,並執行代碼,你們可以幫忙嗎?

萬一任何你想看到的,這裏是爲PlanetFragment

public static class PlanetFragment extends Fragment { 
    public static final String ARG_PLANET_NUMBER = "planet_number"; 

    public PlanetFragment() { 
     // Empty constructor required for fragment subclasses 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_planet, container, false); 
     int i = getArguments().getInt(ARG_PLANET_NUMBER); 
     String planet = getResources().getStringArray(R.array.planets_array)[i]; 

     int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), 
         "drawable", getActivity().getPackageName()); 
     ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId); 
     getActivity().setTitle(planet); 
     return rootView; 
    } 
} 

回答

1

找到一種方法來保留在動作條的frag冠軍的方向變化,不知道最好的辦法子類,但似乎對我來說很乾淨。

public class Example extends Fragment{ 

private String[] titleList; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    titleList = getResources().getStringArray(R.array.sell_array); 
    getActivity().setTitle(titleList[0]); 

    return inflater.inflate(R.layout.fish, container, false); 
} 


} 

每個片段都有各自的陣列位置編號。 另外,從主要活動課,我拿出了不必要的

setTitle(titleList[position]); 
從任何抽屜式導航項目的選擇

,因爲現在從各個片段類

7

處理從Navigation drawer example app,在這裏我做了什麼:

  • 您的活動的onSaveInstanceState()

    @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); CharSequence title = mDrawerLayout.isDrawerOpen(mLeftDrawer) ? mDrawerTitle:mTitle; outState.putCharSequence(KEY_STATE_TITLE, title); }

  • 您的活動的onCreate()

    if (saveInstanceState!=null) { setTitle(savedInstanceState.getCharSequence(KEY_STATE_TITLE)); }

希望這有助於。

+0

它應該是'getActivity()。setTitle()' –

4

@ maohieng的回答對我有幫助,但是如果抽屜在方向更改(它是屏幕名稱,而不是應用程序的標題)上打開,則會設置錯誤的標題。所以我實現了這個完美解決問題的代碼。

onSaveInstanceState()我使用布爾值來檢查它的抽屜是否打開。似乎很奇怪,但方向更改後isDrawerOpen(mDrawer)返回false當我把它上的onCreate():

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putCharSequence("title", mTitle); 
    outState.putBoolean("isDrawerOpen", 
      mDrawerLayout.isDrawerOpen(mDrawerLinear)); 
} 

在活動的onCreate()

if (savedInstanceState == null) { 
    selectItem(YOUR_FRAGMENT); 
} else { 
    if (savedInstanceState.getBoolean("isDrawerOpen")) { 
     mTitle = savedInstanceState.getCharSequence("title"); 
    } else { 
     setTitle(savedInstanceState.getCharSequence("title")); 
    } 
} 
0

你必須添加的setTitle(menuItem.getTitle()) ;如下圖所示,

@覆蓋 公共布爾onNavigationItemSelected(菜單項菜單項){

  //Checking if the item is in checked state or not, if not make it in checked state 
      if (menuItem.isChecked()) menuItem.setChecked(false); 
      else menuItem.setChecked(true); 


      **setTitle(menuItem.getTitle());** 
      //Closing drawer on item click 
      drawerLayout.closeDrawers(); 

希望這有助於。