安卓标签布局setContent

7
我正在开发一个带有选项卡布局的Android应用程序。我已经让它不像Google的教程建议的那样生成新的活动,但这只是为了在单击每个选项卡时显示我的内容。目前无论哪个选项卡处于活动状态,都只显示黑色。

以下是我的最新代码:
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

我只包含这两个,它们都是完全相同的,对于LinearLayout的android:id参数具有适当的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>

这里有一个切换选项卡时所看到的截图:

订单选项卡

alt text

持仓选项卡

alt text

无论是在模拟器还是在我刚得到的三星Galaxy上都是如此,顺便推荐一下,有任何想法吗?

1个回答

5

我找到了答案,只需要在main.xml文件中增加一个简单的参数即可:

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,它现在包括参数android:orientation。之前它的作用是将其他所有内容推到屏幕右侧。现在它按照应有的方式工作。

注意:明天我会在办公室测试是否仍然允许我旋转手机并在横向方向上正确显示。如果不行,那么我可能需要为水平方向创建一个单独的xml文件,除非当然有人对此有深入的了解。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接