我正在开发带有选项卡式布局的Android应用程序。我已经知道它没有产生像Google's tutorial suggested这样的新活动,但是我只是在点击每个标签时努力让我的内容显示。目前它只显示黑色,无论选项卡处于活动状态。Android选项卡式布局setContent
以下是我在其最新的迭代代码:
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参数。
<?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>
而这里的选项卡之间切换时,我得到什么的截图:
订单标签
位置标签
对于模拟器和我刚刚买到的三星Galaxy都是如此,我非常推崇顺便提一下,有什么想法?
+1为答案和一个非常好的解释。 – Subby 2014-02-24 12:05:57