2014-09-24 88 views
1

这是我的activity_main.xml布局,它包含两个选项卡。所选标签内容根本没有显示。我哪里错了?如果我在下面的布局中交换FragmentTabHost和Framelayout的位置,即使制表符出现在底部,则显示标签内容。但我希望我的标签位于屏幕顶部。请帮帮我。FragmentTabHost选项卡的内容未显示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<android.support.v4.app.FragmentTabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="match_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" /> 

</LinearLayout> 

MainActivity

public class MainActivity extends FragmentActivity { 
private FragmentTabHost mTabHost; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

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

    LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    View v1 = inflator.inflate(R.layout.tab_bg, null); 
    TextView tv1 = (TextView)v1.findViewById(R.id.tabTitleTextView); 
    tv1.setText("Tab1"); 

    View v2 = inflator.inflate(R.layout.tab_bg, null); 
    TextView tv2 = (TextView)v2.findViewById(R.id.tabTitleTextView); 
    tv2.setText("Tab2"); 

    mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator(v1), FragmentTab.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator(v2), FragmentTab.class, null); 
    mTabHost.setCurrentTab(0);  
} 

} 

FragmentTab:

public class FragmentTab extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_layout, null); 
    TextView tv = (TextView) v.findViewById(R.id.text); 
    tv.setText(this.getTag() + " Content"); 
    return v; 
} 
} 
+0

后的java文件还 – 2014-09-24 13:22:46

+0

@MehulJoisar:已编辑的问题 – user1670443 2014-09-24 13:25:13

回答

4

试试这个:

<android.support.v4.app.FragmentTabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<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="wrap_content" 
     android:layout_weight="0" 
     android:orientation="horizontal" /> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0" /> 

    <FrameLayout 
     android:id="@+id/realtabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 
</LinearLayout> 

,改变

mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 

mTabHost.setup(this, getSupportFragmentManager(),R.id.realtabcontent); 
+0

我无法相信这个固定的蹩脚的问题,我是16个设备面临API – vanomart 2016-08-29 20:21:22

相关问题