Android的标签式布局setContent [英] Android Tabbed Layout setContent

查看:113
本文介绍了Android的标签式布局setContent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序与标签布局。我有它的地方它不象产卵谷歌的教程一个新的活动提示,但是我只能这样做是为了让我的内容在每个选项卡上,单击时显示。目前,它只是显示黑色不管是什么标签被激活。

I'm developing an Android application with a tabbed layout. I've got it to where it doesn't spawn a new activity like Google's tutorial suggested, however I only did this in an effort to get my content to show when clicking on each tab. Currently it just shows black regardless of what tab is active.

以下是我的code在其最新的迭代:

The following is my code in its latest iteration:

Main.java

public class Main extends TabActivity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;

        // add orders tab
        spec = tabHost.newTabSpec("orders").setIndicator("Orders",
                          res.getDrawable(R.drawable.flash_36))
                          .setContent(R.id.ordersLayout);
        tabHost.addTab(spec);

        // add positions tab
        spec = tabHost.newTabSpec("positions").setIndicator("Positions",
                          res.getDrawable(R.drawable.small_tiles_36))
                          .setContent(R.id.positionsLayout);
        tabHost.addTab(spec);

        // add strategies tab
        spec = tabHost.newTabSpec("strategies").setIndicator("Strategies",
                          res.getDrawable(R.drawable.cards_36))
                          .setContent(R.id.strategiesLayout);
        tabHost.addTab(spec);

        // add account tab
        spec = tabHost.newTabSpec("account").setIndicator("Account",
                          res.getDrawable(R.drawable.seal_36))
                          .setContent(R.id.accountLayout);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(1);
    }

}

的main.xml

<?xml version="1.0" encoding="utf-8"?>


<TabHost    android:id="@android:id/tabhost" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android">

            <LinearLayout   android:id="@+id/mainLayout" 
                            android:layout_width="fill_parent" 
                            android:layout_height="fill_parent"
                            android:padding="5dp">

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

                <FrameLayout android:id="@android:id/tabcontent" 
                            android:layout_width="fill_parent" 
                            android:layout_height="fill_parent"
                            android:background="#fff"
                            android:padding="5dp">

                            <include layout="@layout/orders"/>
                            <include layout="@layout/positions"/>
                            <include layout="@layout/strategies"/>
                            <include layout="@layout/account"/>

                </FrameLayout>

            </LinearLayout>

</TabHost>

Account.xml在

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    android:id="@+id/accountLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#fff"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView 
        android:text="Account Information" 
        android:id="@+id/accountLabel" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">

    </TextView>

</LinearLayout>

Orders.xml

我只包括这两种,它们都是完全一样的适当IDS的LinearLayout中的android:id参数

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    android:id="@+id/ordersLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#fff"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView 
        android:text="Working Orders" 
        android:id="@+id/ordersLabel" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">

    </TextView>

</LinearLayout>

这是一个什么样的选项卡之间切换时,我得到一个截图:

And here's a screenshot of what I get when switching between tabs:

订单标签

位置标签

这是真正为仿真器和三星Galaxy我刚刚和我的方式非常reccommend,任何想法?

This is true for both the emulator and a Samsung Galaxy I just got, and I highly reccommend by the way, any ideas?

推荐答案

我想它了,它的一个简单的参数除了main.xml中的文件:

I figured it out, and it's one simple parameter addition to the main.xml file:

的main.xml(修订)

<?xml version="1.0" encoding="utf-8"?>


<TabHost    android:id="@android:id/tabhost" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android">

            <LinearLayout   android:id="@+id/mainLayout" 
                            android:layout_width="fill_parent" 
                            android:layout_height="fill_parent"
                            android:orientation="vertical"
                            android:padding="5dp">

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

                <FrameLayout android:id="@android:id/tabcontent" 
                            android:layout_width="fill_parent" 
                            android:layout_height="fill_parent"
                            android:background="#fff"
                            android:padding="5dp">

                            <include layout="@layout/orders"/>
                            <include layout="@layout/positions"/>
                            <include layout="@layout/strategies"/>
                            <include layout="@layout/account"/>

                </FrameLayout>

            </LinearLayout>

</TabHost>

注意在的LinearLayout ,现在包括参数的安卓方向的。这是什么做之前,是推动一切关闭屏幕右侧。现在,它的作品,因为它应该。

Notice the LinearLayout, it now includes the parameter android:orientation. What it was doing before was pushing everything else off screen to the right. Now it works as it should.

注意:在办公室明天我可以测试是否这仍然让我转电话,有它在横向方向正确显示。如果没有,那么我可能会创造一个横向单独的XML文件,除非有人洞察到这门课程。

Note: At the office tomorrow I can test whether this still allows me to rotate the phone and have it display correctly in landscape orientation. If not, then I'll probably have to create a separate xml file for horizontal, unless someone has insights into this of course.

这篇关于Android的标签式布局setContent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆