这是我在Android选项卡上看到的一种奇怪的行为。或者可能是仿真器的错!下面是布局的XML。
为每个选项卡分别设计的布局是res/layout/tab1.xml和res/layout/tab2.xml .而且一切看起来都很好,除了当它绘制的时候,它重叠标签的一些方法。如果选择TAB1,一切看起来都正常,但如果选择TAB2,它只会绘制tab1布局。如果我回到tab1,那么一切看起来都很正常。
那么为什么它要从tab1中绘制布局,而不是从一个黑色/空白的屏幕开始呢?在绘制选项卡2的布局和内容之前,TabWidget不是应该处理这些情况并调用clear()方法吗?
我正在模拟一台拥有Android2.2 (API级别8)的机器。
<?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"
android:padding="5dp">
<include layout="@layout/tab1" />
<include layout="@layout/tab2" />
</FrameLayout>
</LinearLayout>
</TabHost>编辑:根据请求,我将添加tab2布局,这基本上是一个空白屏幕!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>所以如果我转到tab2.它只是在表1的控件(按钮等)上绘制(添加字符串)。而不是从一个空白/黑色屏幕开始吗?
发布于 2011-03-09 13:18:58
解决了问题!
您不需要包括(我刚刚从以前的main.xml中删除了它们)
<include layout="@layout/tab1" />
<include layout="@layout/tab2" />因为它将重叠来自tab1和tab2的GUI元素。相反,将main.xml与tabwidget和framelayout放在一起,就像官方教程中的那样。
您需要做的是在各自的类中调用: setContentView(R.layout.tab1)和setContentView(R.layout.tab2),在onCreate方法中!就是这样。这就是添加控件(按钮、发短信)的方式。在每个选项卡上(这本身就是一个活动)
现在它就像一种魅力!
https://stackoverflow.com/questions/5230993
复制相似问题