Android:tabactivity - 所有选项卡的内容首先重叠

发布于 2024-12-06 05:48:30 字数 3459 浏览 0 评论 0原文

我正在测试 TabActivity,每个选项卡中都有一个列表。

运行应用程序时,选项卡的内容会像这样重叠。 在此处输入图像描述

单击选项卡后,重叠会被清除。 这是我的代码:

testtabs.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">
        <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">
            <ListView
                android:id="@+id/list1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
            </ListView>
            <ListView
                android:id="@+id/list2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
            </ListView>
        </FrameLayout>
    </LinearLayout>
</TabHost>

和测试活动

public class TabbedActivity extends TabActivity {

    private static final String LIST1_TAB_TAG = "List1";
    private static final String LIST2_TAB_TAG = "List2";
    private ListView listView1;
    private ListView listView2;
    private TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testtabs);
        tabHost = getTabHost();

        // setup list view 1
        listView1 = (ListView) findViewById(R.id.list1);
        // create some dummy strings to add to the list
        List<String> list1Strings = new ArrayList<String>();
        list1Strings.add("List 11");
        list1Strings.add("List 12");
        list1Strings.add("List 13");
        list1Strings.add("List 14");
        listView1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));

        // setup list view 2
        listView2 = (ListView) findViewById(R.id.list2);
        List<String> list2Strings = new ArrayList<String>();
        list2Strings.add("Test2 List 21");
        list2Strings.add("Testing 22");
        list2Strings.add("More test 23");
        list2Strings.add("Test Again 24");

        listView2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list2Strings));

        // add views to tab host
        tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory() {
            public View createTabContent(String arg0) {
                return listView1;
            }
        }));
        tabHost.addTab(tabHost.newTabSpec(LIST2_TAB_TAG).setIndicator(LIST2_TAB_TAG).setContent(new TabContentFactory() {
            public View createTabContent(String arg0) {
                return listView2;
            }
        }));
        tabHost.setCurrentTab(0);
    }
}

I was testing TabActivity with a list in each tab.

While running the app, the contents of the tabs gets overlapped like this.
enter image description here

After i click on the tabs the overlapping gets cleared.
Here is my code :

testtabs.xml layout :

<?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">
        <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">
            <ListView
                android:id="@+id/list1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
            </ListView>
            <ListView
                android:id="@+id/list2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
            </ListView>
        </FrameLayout>
    </LinearLayout>
</TabHost>

And Test Activity

public class TabbedActivity extends TabActivity {

    private static final String LIST1_TAB_TAG = "List1";
    private static final String LIST2_TAB_TAG = "List2";
    private ListView listView1;
    private ListView listView2;
    private TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testtabs);
        tabHost = getTabHost();

        // setup list view 1
        listView1 = (ListView) findViewById(R.id.list1);
        // create some dummy strings to add to the list
        List<String> list1Strings = new ArrayList<String>();
        list1Strings.add("List 11");
        list1Strings.add("List 12");
        list1Strings.add("List 13");
        list1Strings.add("List 14");
        listView1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));

        // setup list view 2
        listView2 = (ListView) findViewById(R.id.list2);
        List<String> list2Strings = new ArrayList<String>();
        list2Strings.add("Test2 List 21");
        list2Strings.add("Testing 22");
        list2Strings.add("More test 23");
        list2Strings.add("Test Again 24");

        listView2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list2Strings));

        // add views to tab host
        tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory() {
            public View createTabContent(String arg0) {
                return listView1;
            }
        }));
        tabHost.addTab(tabHost.newTabSpec(LIST2_TAB_TAG).setIndicator(LIST2_TAB_TAG).setContent(new TabContentFactory() {
            public View createTabContent(String arg0) {
                return listView2;
            }
        }));
        tabHost.setCurrentTab(0);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

归途 2024-12-13 05:48:30

您可以从 xml 布局中删除这两个 ListView,然后在 java 代码中创建它们。

例如 listView1 = new ListView(this);

一切都会好起来的。

You can remove both ListView from xml layout and just create them in java code.

e.g. listView1 = new ListView(this);

Everything will be Ok.

冬天旳寂寞 2024-12-13 05:48:30

我发现的另一个解决方案是将以下标记添加到布局 XML 文件中的两个列表视图中:

android:visibility="invisible"

Another solution I found is to add the following tag to both the listviews in the layout XML file:

android:visibility="invisible"
长安忆 2024-12-13 05:48:30

您在 FrameLayout 中使用了两个 ListView,这就是重叠的原因。

如果你希望你应该有一个ListView在另一个ListView下面,将ListView保留在LinearLayout中,如下所示,

<?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">
        <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">

             <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
            <ListView
                android:id="@+id/list1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
            </ListView>
            <ListView
                android:id="@+id/list2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
            </ListView>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

You are taking two ListView's in a FrameLayout that is the reason for your over-lapping.

If you want that you should have one ListView below the other keep the ListView's inside the LinearLayout like this,

<?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">
        <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">

             <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
            <ListView
                android:id="@+id/list1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
            </ListView>
            <ListView
                android:id="@+id/list2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1">
            </ListView>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文