问题描述
我目前编码使用了大量碎片访问采用抽屉式导航的应用程序。到目前为止好,但我也希望有内部的片段中的一个有2个选项卡一TabHost。我最好如何实现它?这是一个code片断:
I'm currently coding an application that uses a lot of Fragments accessible using a Navigation Drawer. So far so good, but I also want to have a TabHost with 2 Tabs inside one of the Fragments. How do I best implement it? This is a code snippet:
public class SectionFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int position = getArguments().getInt("position");
if (position == 0) {
rootView = inflater.inflate(R.layout.startmenu_layout, container,
false); // die rootView zum Weiterarbeiten holen
} else if (position == 1) {
rootView = inflater.inflate(R.layout.startmenu_layout, container,
false);
等等...
我如何进行最好?
由于提前,
How do I proceed best?Thanks in advance,
forumfresser
forumfresser
推荐答案
下面是一个code我用我的TabHost
我知道你的感觉会导致它真的很难找到一个工作指南和工作的例子在那里...
here is a code i use for my TabHosti know how you feel cause it's really hard to find a working tutorial and a working example out there...
反正
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);//here you define which tab you want to setup
spec.setIndicator("So Close");//here you choose the text showed in the tab
tabs.addTab(spec);
spec=tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("Contacts");
tabs.addTab(spec);
和这里是XML code我用
and here is the xml code I use
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/setting" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="46dp"
android:background="@drawable/transparent_white" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
</LinearLayout>
</TabHost>
请确保你改变的背景下的 TabWidget 根据您的资源
make sure you change the background of the TabWidget according to your res
您可以把XML code,无论你在布局文件,RelativeLayout的或希望的LinearLayout ...你可以更改选项卡中显示的数据的类型...希望我能帮助:)
you can put the xml code wherever you want in your layout file, a RelativeLayout or a LinearLayout... and you can change the type of the data shown in the tabs... Hope I could help :)
这篇关于Android的TabHost里面只有一个片段(NavigationDrawer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!