2013-03-21 97 views
3

我使用帶有FragmentStatePagedAdapter的選項卡。下面的代碼是從onOptionsItemSelected(MenuItem item)方法:FragmentTransaction替換整個屏幕

case R.id.menu_help: 
    System.out.println("Pressin' da help button!"); 
    FragmentManager mananger = getSupportFragmentManager(); 
    android.support.v4.app.FragmentTransaction trans = mananger.beginTransaction(); 
    trans.replace(android.R.id.content, new HelpFragment()); 
    trans.addToBackStack(null); 
    trans.commit(); 
    break; 

android.R.id.content將然而只更換動作條下方的視圖(和覆蓋它在製表片段)。 是否有一個簡單的方法,如何用一個片段替換整個屏幕(不應該有一些其他的系統資源?),而不必爲此創建一個新的活動?或者新的活動會更好嗎?

在此先感謝您的幫助

容器佈局(從MainActivity啓動開始):

<android.support.v4.view.ViewPager 
    android:id="@+id/masterViewPager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

我猜這是類HelpFragment的:

public class HelpFragment extends Fragment { 

    private View view; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle states) { 
    view = inflater.inflate(R.layout.layout_help_screen, container, false); 

    return view; 
    } 

} 

和碎片XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/nothing" /> 

</LinearLayout> 
+0

顯示你的XML代碼... – Developer 2013-03-21 14:31:10

+0

我在xml中做得不是很多。你想看什麼? – AreusAstarte 2013-03-21 14:33:26

+0

希望看到片段的大小和你轉移它的地方... – Developer 2013-03-21 14:34:05

回答

0

更新:下面的開關不再需要,如果你更新程序兼容性-V7修訂19.0.0。或更新版本。請致電issue 59077瞭解更多信息。


我懷疑你正在使用運行的Android版本設備/仿真器之前 4.x版(Ice Cream Sandwich的,API等級14)。當使用分段事務時,我遇到了類似的覆蓋問題。出現佈局問題是因爲在Android 2.x和3.x上與Android 4.x相比,內容視圖的引用有所不同。請嘗試更改您的代碼:

trans.replace(android.R.id.content, new HelpFragment()); 

到:

trans.replace(getContentViewCompat(), new HelpFragment()); 

...

public static int getContentViewCompat() { 
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH ? 
       android.R.id.content : R.id.action_bar_activity_content; 
} 

更多的背景信息可以在post of Shellum找到。

+0

我當時正在調試Android 4.1.2 Galaxy S3上的應用程序。幾個月前我已經離開這個項目,但也許會再次挖掘整個事情來測試你的解決方案。我應該關閉這個問題:/ – AreusAstarte 2013-10-21 12:41:33