充气相同的布局的多个实例为选项卡 [英] Inflating multiple instances of the same layout into tabs

查看:221
本文介绍了充气相同的布局的多个实例为选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是困扰了我好几天,现在的一个问题。我已经做了我最好寻找一个解决方案,但无论我走到哪里它说我目前使用的方法是正确的。

这种情况很简单:我有五个选项卡,这是(通过使用的setText更改标签之​​一为例)含有略有不同的内容相同的布局。因此,我膨胀的布局五倍,并添加它们作为单独的标签。

这是我的code:

  com.test包;进口android.app.TabActivity;
进口android.content.res.Resources;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.TabHost;
进口android.widget.TextView;公共类测试活动扩展TabActivity
{
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        TabHost主机= getTabHost();
        资源解析度= getResources();        的String []天= {星期一,星期二,星期三,星期四,星期五};        的for(int i = 0; I< days.length;我++)
        {
            查看内容= getLayoutInflater()膨胀(R.layout.test,host.getTabContentView(),TRUE)。            ((的TextView)contents.findViewById(R.id.title))的setText(天[I]);            host.addTab
            (
                host.newTabSpec(天[I])。
                setIndicator(天[I])。
                setContent(co​​ntents.getId())
            );            。host.getTabWidget()getChildAt(I).getLayoutParams()高度= 55。
        }
    }
}

和我的布局文件看起来像这样:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:填充=6DIP>    <的TextView
        机器人:ID =@ + ID /标题
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=测试
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>< / LinearLayout中>

的问题在于,代替文本正对单独的片,所有的文字重叠海誓山盟并且是为每一个标签是相同的。这可能是很简单的东西是我俯瞰。任何帮助是AP preciated。


解决方案

 所有文字重叠海誓山盟,是每一个标签相同

他们不会从我的发现相互重叠。如果删除安卓:从您的布局文件中的文本=测试,你会看到只有周五在所有选项卡。

看起来你需要使用 TabContentFactory 来实现你想要的。这是我的问题的解决方案。它会给你你需要的东西。

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    最后TabHost主机= getTabHost();
    资源解析度= getResources();    最终的String []天= {星期一,星期二,星期三,星期四,
            星期五};    的for(int i = 0; I< days.length;我++){
        TabHost.TabSpec规格= host.newTabSpec(天[I]);
        spec.setContent(新TabHost.TabContentFactory(){
            公共查看createTabContent(字符串标签){
                查看WV = getLayoutInflater()膨胀(R.layout.test,host.getTabContentView(),FALSE)。
                ((的TextView)wv.findViewById(R.id.title))的setText(标签)。
                返回(WV);
            }
        });
        spec.setIndicator(天[I]);
        host.addTab(规范);
    }
    host.setCurrentTab(0);
}

下面是我上手 HTTPS链接:// github上。 COM / commonsguy / CW-的Andr​​oid /树/主/花式/ DynamicTab

幸得马克·墨菲(Commonsguy)。

This is a problem that has troubled me for a few days now. I've done my best searching for a solution, but everywhere I go it says the approach I'm currently using is the right one.

The situation is simple: I have five tabs, which are to contain the same layout with slightly different content (for example by using setText to change one of the labels). As such, I am inflating the layout five times, and adding all of them as seperate tabs.

This is my code:

package com.test;

import android.app.TabActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

public class TestActivity extends TabActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost host = getTabHost();
        Resources res = getResources();

        String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

        for (int i = 0; i < days.length; i++)
        {
            View contents = getLayoutInflater().inflate(R.layout.test, host.getTabContentView(), true);

            ((TextView)contents.findViewById(R.id.title)).setText(days[i]);

            host.addTab
            (
                host.newTabSpec(days[i]).
                setIndicator(days[i]).
                setContent(contents.getId())
            );

            host.getTabWidget().getChildAt(i).getLayoutParams().height = 55;
        }
    }
}

And my layout file looks like this:

<?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"
    android:orientation="vertical"
    android:padding="6dip">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

The problem is that, instead of the text being on seperate tabs, all the text overlaps eachother and is the same for every tab. It might be something really simple that I'm overlooking. Any help is appreciated.

解决方案

 all the text overlaps eachother and is the same for every tab

They ain't overlapping each other from my discovery. If you remove android:text="Test" from your layout file, you will be seeing only Friday in all tab.

It looks like you will need to use TabContentFactory to achieve what you want. Here is my solution to your problem. It will give you what you needed.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final TabHost host = getTabHost();
    Resources res = getResources();

    final String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday" };

    for (int i = 0; i < days.length; i++) {
        TabHost.TabSpec spec=host.newTabSpec(days[i]);
        spec.setContent(new TabHost.TabContentFactory() {
            public View createTabContent(String tag) {
                View wv = getLayoutInflater().inflate(R.layout.test, host.getTabContentView(), false);
                ((TextView) wv.findViewById(R.id.title)).setText(tag);
                return(wv);
            }
        });
        spec.setIndicator(days[i]);
        host.addTab(spec);
    }
    host.setCurrentTab(0);
}

Here is the link where I get started https://github.com/commonsguy/cw-android/tree/master/Fancy/DynamicTab

Credit goes to Mark Murphy(Commonsguy).

这篇关于充气相同的布局的多个实例为选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆