2013-03-21 96 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