Android选项卡:第二个选项卡的内容隐藏在第一个选项卡的内容下
我正在尝试创建一个具有两个选项卡的活动的应用程序,其中tab1上面有一张图片和一些文本,tab2有两个用户可以单击的按钮(这将导致进一步的活动) 由于某种原因,我无法显示tab2上的按钮;我所能看到的只是tab1中的同一张图片。仔细检查后,我注意到按钮隐藏在tab1的图片下面。我做错了什么?我怎样才能让那张照片消失而不出现在tab2上 以下是我的代码: 指数活性Android选项卡:第二个选项卡的内容隐藏在第一个选项卡的内容下,android,view,tabs,Android,View,Tabs,我正在尝试创建一个具有两个选项卡的活动的应用程序,其中tab1上面有一张图片和一些文本,tab2有两个用户可以单击的按钮(这将导致进一步的活动) 由于某种原因,我无法显示tab2上的按钮;我所能看到的只是tab1中的同一张图片。仔细检查后,我注意到按钮隐藏在tab1的图片下面。我做错了什么?我怎样才能让那张照片消失而不出现在tab2上 以下是我的代码: 指数活性 public class IndexActivity extends TabActivity { @Override publ
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>
<?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>
<?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>
<?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">
<LinearLayout android:id="@+id/tab1content"
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: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>
</LinearLayout>
</merge>
tab1.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>
<?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>
<?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>
<?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">
<LinearLayout android:id="@+id/tab1content"
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: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>
</LinearLayout>
</merge>
tab2是这样的:
<?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>
<?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>
<?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>
<?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">
<LinearLayout android:id="@+id/tab1content"
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: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>
</LinearLayout>
</merge>
请帮忙!我一定忽略了什么,但我想不出是什么:(我建议你看看这个 你的例子实际上很好地代表了这个例子 我希望你已经看到了…这是我第一次开发我的第一个android标签小部件时使用的
PS:一定要看一下上面的这个例子……继续你需要知道的每一件事。看看你的代码和XML,我想问题在于tab1.XML的这一行:
<TextView android:id="@+id/tab1content"...
从
main.xml
中的FrameLayout
中删除以下两行,然后查看发生了什么
<include layout="@layout/tab1"/>
<include layout="@layout/tab2"/>
Hey按钮现在确实显示在tab2上;但是,tab1中的文本“@string/text”不再可见:(是的,这与你的布局是一个不同的问题,你应该在这个线程中标记答案,并针对新问题提出一个新问题。我可能也做了其他事情,但我将android:src更改为android:background,解决了我的问题…但是你是否尝试过看看发生了什么?原因是你多次添加视图。布局调用setContentView(R.layout.main)
时,将自动添加tab1.xml
和tab2.xml
中的ts,但随后将创建tabspec
,其中第一个包含TextView
(tab1content)从tab1.xml
开始,第二个包含从tab2.xml
(tab2content)开始的整个布局。当您调用spec.setContent(…)
时,您手动重复setContentView(R.layout.main)的布局文件内容的膨胀
已经完成,因为main.xml
中的元素。我取出了包含文件,应用程序根本无法启动(强制关闭);奇怪的是,我似乎可以通过将安卓:src更改为安卓:background来解决问题。但你所做的非常有意义……这是否意味着我实际上有4个选项卡,尽管我只看到了两个?我不知道我在做什么:(否-您有两个选项卡,但我认为您在每个选项卡中添加了两次一些选项卡内容。您不需要在框架布局中添加任何内容,除非您希望它出现在每个选项卡中。有一件事我不明白,就是在tab1.xml中使用
,为什么它不只是一个标准的线性布局
?我也不明白您为什么要这样做移动
元素会导致强制关闭-正如我所说,您不需要XML文件中的框架布局中的任何内容-跟踪导致它的异常。我甚至不知道我做了什么,但我现在一切都正常了!我正在按照本指南使用元素…我仍然有我的元素,但我会尝试将它们带到您的文件夹中如果是的,你介意指出我的答案作为你问题的答案吗如果不是,你能告诉我你试过什么吗,