0

我在抽屉里创建了一个TabHost。这意味着我创建了3个Fragmentxml个文件:ExhibitorFragment,TabInternationalFragmentTabLocalFragment。现在的问题是,我看不到我的tabwidget在Toolbar下,即使它在任何两个片段中显示设计xmlTabwidget在android studio的工具栏/导航栏下不可见

下面是我ExhibitorFragment.java代码:

public class ExhibitorFragment extends Fragment{ 


public ExhibitorFragment() { 
    // Required empty public constructor 
} 

private FragmentTabHost mTabHost; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    mTabHost = new FragmentTabHost(getActivity()); 
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_exhibitor); 

    Bundle arg1 = new Bundle(); 
    arg1.putInt("Arg for Frag1", 1); 
    mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"), 
      TabInternationalFragment.class, arg1); 

    Bundle arg2 = new Bundle(); 
    arg2.putInt("Arg for Frag2", 2); 
    mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"), 
      TabLocalFragment.class, arg2); 


    return mTabHost; 
} 

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    mTabHost = null; 
} 

} 

fragment_exhibitor.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.guitarista.citem.Example"> 

    <TabHost android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 

       /> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 

       > 

       <FrameLayout 
        android:id="@+id/International" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        /> 

       <FrameLayout 
        android:id="@+id/Local" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        /> 


      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</FrameLayout> 

TabInternationalFragmentTabLocalFragment设置为默认为他们尊敬的xml创建相同的时(fragment_tab_international,fragment_tab_local)。

回答

0

Java代码是正确的尝试XML文件更改为这种格式

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_marginTop="55dp" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="45dp"/> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 
+0

我有渲染问题。这是正常的吗? – SovietSenpai

+0

是的,这是完全正常的....布局将只在运行时可见 – qwertygamer

+0

仍然无法工作:( – SovietSenpai

0

USR这个代码

<TabHost android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 

       /> 

      <FrameLayout 
       android:layout_below="@android:id/tabs" 
       android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 

       > 

       <FrameLayout 
        android:id="@+id/International" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        /> 

       <FrameLayout 
        android:id="@+id/Local" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        /> 


      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</FrameLayout> 
+0

这是什么区别?我似乎无法 – SovietSenpai

+0

android:layout_below =“@ android:id/tabs” –

0
FragmentTabHost mTabHost; 
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 

    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("tab2"), 
      tab1.class,null); 

    mTabHost.addTab(mTabHost.newTabSpec("tab2") 
      .setIndicator("tab2"), tab2.class, null); 
+0

生病了试试吧.. – SovietSenpai

+0

还是没有运气的兄弟,可能很难翻译一些代码,因为Im在片段中实现这些而不是MainActivity本身。 – SovietSenpai

+0

编译并尝试一下 compile'c​​om.android.support:design:24.2.1' compile'c​​om.android.support:support-v4:24.2.1' – qwertygamer