Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 相互重叠的碎片_Android_Android Fragments_Android Actionbar_Fragment - Fatal编程技术网

Android 相互重叠的碎片

Android 相互重叠的碎片,android,android-fragments,android-actionbar,fragment,Android,Android Fragments,Android Actionbar,Fragment,我有一个带有3个选项卡的操作栏,每个选项卡打开一个片段。第三个选项卡“目录”有一个列表: 当我单击一个项目时,它会打开另一个片段,它不是操作栏的一部分: public void onClick(View v) { switch (v.getId()) { case R.id.category1: Fragment cosmeticsFragment = new ActivityCosmetics(); FragmentTransa

我有一个带有3个选项卡的操作栏,每个选项卡打开一个片段。第三个选项卡“目录”有一个列表:

当我单击一个项目时,它会打开另一个片段,它不是操作栏的一部分:

public void onClick(View v) {
    switch (v.getId())
    {
    case R.id.category1:    
        Fragment cosmeticsFragment = new ActivityCosmetics();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        transaction.replace(android.R.id.content, cosmeticsFragment);
        transaction.addToBackStack(null);

        transaction.setTransition(1);

        transaction.commit();
        break;
        ...
这就是它之后的样子:

从这一点来看,如果我转到“其他”选项卡,然后返回到“目录”选项卡,我会看到前面的两个片段相互重叠:


如何防止这种情况发生?

您可以通过标记搜索片段来管理它们。向backstack添加片段时,添加标记名

transaction.addToBackStack("myCustomFragmentTag");
如果要销毁应用程序中的任何位置的碎片:

Fragment previousInstance = getFragmentManager().findFragmentByTag("myCustomFragmentTag");
                if (previousInstance != null)
                    transaction.remove(previousInstance);
您可以尝试重写某些行为,以便这行代码将销毁初始化的最后一个片段

getFragmentManager().popBackStack(); 

你可以像这样尝试标签

在布局xml中


将背景设置为片段布局将解决此问题。

根据此问题:

您只需在XML文件中设置背景色即可


解决这个问题。

我尝试如下使用它:
transaction.addToBackStack(“CategoryFragment”)
然后尝试在onResume和OnAbreselected方法中使用其余的代码。但我仍然看到这些片段重叠…这应该是一个注释。android:background=“?android:colorBackground”您还必须添加以下内容,以便单击不会传递到旧片段==>android:clickable=“true”将背景添加到片段将不是正确的解决方案。它只是一个黑客
   public class Tabs extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tabs);
    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
   // Intent intent;  // Reusable Intent for each tab
    // Create an Intent to launch an Activity for the tab (to be reused)
    Intent intent1 = new Intent().setClass(this, Social.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("app_name").setIndicator("Practice",
                      res.getDrawable(R.drawable.tab_social))
                  .setContent(intent1);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.bbgg);
    // Do the same for the other tabs
    Intent intent2 = new Intent().setClass(this, Web.class);
    spec = tabHost.newTabSpec("application").setIndicator("Application",
                      res.getDrawable(R.drawable.tab_web))
                  .setContent(intent2);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.bbgg);
    Intent intent3 = new Intent().setClass(this, Catalog.class);
    spec = tabHost.newTabSpec("toplinks").setIndicator("Top Links",
                      res.getDrawable(R.drawable.tab_catalog))
                  .setContent(intent3);
    tabHost.addTab(spec);
    tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.bbgg);
    tabHost.setCurrentTab(0);
}
}
    <TabHost 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

  <LinearLayout 
android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical">
  <TabWidget 
android:id="@android:id/tabs" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"></TabWidget>
 <FrameLayout 
android:id="@android:id/tabcontent" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"></FrameLayout>
 </LinearLayout>
 </TabHost>