这是我的activity_main.xml布局,它包含两个选项卡。根本没有显示所选标签内容。我哪里出错了?如果我在下面的布局中交换FragmentTabHost和Framelayout的位置,即将标签显示在底部,则显示标签内容。但我希望我的标签位于屏幕顶部。请帮我。
<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 :(得分: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);