如何控件添加到选项卡布局的Andr​​oid? [英] How to add controls to a Tab Layout in Android?

查看:121
本文介绍了如何控件添加到选项卡布局的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这个教程<一个href="http://developer.android.com/intl/de/guide/tutorials/views/hello-tabwidget.html">http://developer.android.com/intl/de/guide/tutorials/views/hello-tabwidget.html并已完成它。现在,我真的想加你知道一些控制这些标签就像文本框(文本编辑)。

我如何做到这一点?我去我的mail.xml使用Eclipse作为我的IDE,去布局视图,我现在得到一个NullPointerException异常,所以我甚至不能拖的东西到布局了。

感谢

修改

这是我

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT; TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT&GT;
    &LT;的LinearLayout
        机器人:方向=垂直
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT&GT;
        &LT; TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT/&GT;
        &LT;的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT&GT;
            &LT;的LinearLayout
                机器人:方向=垂直
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT&GT;
                &LT;的TextView
                机器人:ID =@ + ID / textview1
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:文本=这是一个标签/&GT;
                &LT; EditText上的android:文本=机器人:ID =@ + ID / EditText01机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:密码=真正的&GT;&LT; /的EditText&GT;

            &LT; / LinearLayout中&GT;

            &LT;的TextView
                机器人:ID =@ + ID / textview2
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:文本=这是另一个选项卡/&GT;
            &LT;的TextView
                机器人:ID =@ + ID / textview3
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:文本=这是第三个选项卡/&GT;
        &LT; /的FrameLayout&GT;
    &LT; / LinearLayout中&GT;
&LT; / TabHost&GT;
 

解决方案

标签都有点滑稽得到初步的工作,因为有很多的code开销,但一旦你通过努力用自己的方式,他们AREN'牛逼太糟糕了。为了让标签来工作的,让我们开始通过提高你的XML文件,然后我们就可以确保您的code实际加载它们是正确的。

首先,你的XML文件。相反,包括直接在main.xml中的一切,你应该使用包括功能。正如其名称所暗示,这可以让你在一个单独的XML文件中工作,然后将其包含在一个行主。这使得main.xml中文件更容易阅读。所以我们特别希望修改上述文件,使它看起来是这样的:

  //无需更改上面的事
&LT;的FrameLayout
    机器人:ID =@机器人:ID / tabcontent
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT&GT;

    &LT;包括布局=@布局/ TAB1/&GT;
    &LT;包括布局=@布局/ TAB2/&GT;
    //与然而,许多其它选项卡要包括

&LT; /的FrameLayout&GT;
 

然后,您需要创建tab1.xml,tab2.xml等等。这些都是在他们开始用的ViewGroup(即的LinearLayout,RelativeLayout的),它包含任何数量的其他部件的正常XML文件。这些小部件可以像EditTexts,按钮,自定义视图,任何你想要的。唯一的规则是,家长的ViewGroup(一个在顶部)必须有一个唯一的ID在里面,在 Android的方式:ID =@ + ID / someUniqueName。你会用它来指代特定的布局/标签在你的code。因此,例如,这将是:

  tab1.xml
&LT; LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:ID =@ + ID / tab1Layout
    机器人:方向=垂直&GT;

    &LT; TextView的... /&GT;
    &LT; EditText上... /&GT;
&LT; / LinearLayout中&GT;
 

做完这些,大家可以看一下你的code。我想你可能已经得到了这个,但以防万一这里有你想要的:

 公共类YourProject扩展TabActivity {

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        资源RES = getResources();
        TabHost tabHost = getTabHost();

        tabHost.addTab(tabHost.newTabSpec(TAB1)。setIndicator(TAB1称号,
                res.getDrawable(R.drawable.logo1))setContent(R.id.tab1Layout))。

        (...)

        //您也可以填写制表符,像这样一个单独的活动:
        意向意图=新的意图(这一点,YourClass.class);
        tabHost.addTab(tabHost.newTabSpec(TAB2)。setIndicator(另一个称号,
                res.getDrawable(R.drawable.logo2))setContent(意向))。

        tabHost.setCurrentTab(0);
    }
}
 

如上所示,可以设置标签之一的内容是一个单独的活性。在这种情况下,该活动的定义,就像任何其他一个有它自己的类,布局等。通常你不应该这样做,而不是仅仅使用不同的视图(与 setContent(R.id。 tabXLayout),但有时它是必要的。因为如果你希望你的标签之一有一个列表的例子,那么你就需要开始的活动有扩展的ListView,并包括所有的样板$ C $下列表视图。

我希望帮助!

I am following this tutorial http://developer.android.com/intl/de/guide/tutorials/views/hello-tabwidget.html and have completed it. Now I would actually like to add you know some controls to these tabs like textboxes(text edit).

How do I do this? I go to my mail.xml using eclipse as my ide and go to layout view and I now get a NullPointerException so I can't even drag stuff onto the layout anymore.

Thanks

Edit

This is what I have

<?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">            
                <TextView 
                android:id="@+id/textview1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is a tab" />
                <EditText android:text="" android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:password="true"></EditText>

            </LinearLayout>

            <TextView 
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is another tab" />
            <TextView 
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" 
                android:text="this is a third tab" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

解决方案

Tabs are a bit funny to get working initially since there's a lot of code overhead, but once you've worked your way through that they aren't too bad. To get tabs to work, let's start by improving your XML file and then we can make sure your code to actually load them is correct.

First off, your XML file. Instead of including everything directly in your main.xml, you should use the include feature. As the name would suggest, this lets you work on a separate xml file and then include it in your main with one line. This makes the main.xml file much easier to read. So we'd modify your file above to make it look like this:

//No need to change anything above this
<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <include layout="@layout/tab1"/>
    <include layout="@layout/tab2"/>
    //and however many other tabs you want to include

</FrameLayout>

You then need to create tab1.xml, tab2.xml and so forth. These are normal xml files in that they start with a ViewGroup (i.e. LinearLayout, RelativeLayout) which contains any number of other widgets. These widgets can be things like EditTexts, buttons, custom views, whatever you want. The only rule is that the parent ViewGroup (the one at the top) must have a unique ID in it, in the manner of android:id="@+id/someUniqueName". You will use that to refer to that specific layout/tab in your code. So for example, this would be:

tab1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:id="@+id/tab1Layout"
    android:orientation="vertical">

    <TextView ... />
    <EditText ... />
</LinearLayout>

With that done, we can look at your code. I assume you've probably already got this, but just in case here's what you want:

public class YourProject extends TabActivity {

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

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

        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1 title",
                res.getDrawable(R.drawable.logo1)).setContent(R.id.tab1Layout));

        (...)

        //You can also fill tabs with a separate activity like so:
        Intent intent = new Intent(this, YourClass.class);
        tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Another title",
                res.getDrawable(R.drawable.logo2)).setContent(intent));

        tabHost.setCurrentTab(0);
    }
}

As shown above you can set the content of one of the tabs to be a separate activity. In that case, the activity is defined just as any other one with its own Class, layout, etc. Usually you shouldn't do this and instead just use a different View (with setContent(R.id.tabXLayout), but sometimes it's needed. For example if you want one of your tabs to have a list, then you need to start an activity in there that extends ListView, and include all the boilerplate code for ListViews.

I hope that helps!

这篇关于如何控件添加到选项卡布局的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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