gpt4 book ai didi

android - FragmentTabHost 选项卡的内容未出现

转载 作者:行者123 更新时间:2023-11-29 15:58:48 27 4
gpt4 key购买 nike

这是我的 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>

主要 Activity

 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);
}

}

fragment 标签:

 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;
}
}

最佳答案

试试这个:

<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);

关于android - FragmentTabHost 选项卡的内容未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26017969/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com