我正在开发一个带有标签布局的
Android应用程序.我已经把它带到了它不会产生像
Google’s tutorial suggested这样的新活动的地方,但是我只是为了在点击每个标签时显示我的内容.目前它只显示黑色,无论哪个标签处于活动状态.
以下是我最新迭代中的代码:
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>
这是我在标签之间切换时获得的屏幕截图:
订单标签
位置标签
这对于模拟器和我刚刚获得的三星galaxy都是如此,我强烈推荐的方式,任何想法?
解决方法
我想通了,它是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文件,除非有人对此有所了解.