我试图创建一个具有2-tabbed活动的应用程序,其中tab1上有一张图片和一些文本,tab2有两个按钮用户可以点击(这应该导致进一步的活动)Android Tabs:第二个选项卡中的内容隐藏在第一个选项卡的内容下
由于某种原因,我无法获得tab2上的按钮来显示;我只能看到与tab1相同的图片。仔细观察,我发现这些按钮隐藏在tab1的图片下。我究竟做错了什么?我如何让这张照片消失,而不是显示在tab2上?
以下是我的代码:
IndexActivity
public class IndexActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost.TabSpec spec = getTabHost().newTabSpec("tab1");
spec.setContent(R.id.tab1content);
spec.setIndicator("tab1");
getTabHost().addTab(spec);
spec = getTabHost().newTabSpec("tab2");
spec.setContent(R.id.tab2content);
spec.setIndicator("tab2");
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
}
}
我的main.xml文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
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"
/>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include layout="@layout/tab1"/>
<include layout="@layout/tab2"/>
</FrameLayout>
</LinearLayout>
tab1.xml是这样的:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/pic"/>
<ScrollView android:id="@+id/scrolltab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/tab1content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#65000000"
android:text="@string/text"
android:textColor="#ffffffff"
android:textSize="20sp"
android:padding="15dp"
/>
</ScrollView>
</merge>
TAB2是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab2content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.10"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.10"
android:text="Button" />
</LinearLayout>
请帮帮忙!我一定是被忽视的东西,但我想不出我什么sugggest :(
我有点需要这两个布局,虽然... – nubicurio
但你试过看看发生了什么?原因是你多次添加视图。当您调用'setContentView(R.layout.main)'时,将自动添加'tab1.xml'和'tab2.xml'的布局,但是您将创建'TabSpecs',其中第一个包含'TextView'(tab1content )来自'tab1.xml',第二个包含'tab2.xml'(tab2content)的整个布局。当您调用'spec.setContent(...)'时,由于main.xml中的元素,您正手动重复布局文件内容的通货膨胀,因此setContentView(R.layout.main)已完成。 –
Squonk
我拿出包含文件,应用程序根本无法启动(强制关闭);奇怪的是,我似乎通过将android:src更改为android:background来解决问题。但是你做了什么很有意义......这是否意味着我实际上有4个选项卡,即使我只看到两个选项卡?我不知道我在做什么:( – nubicurio