android - 如何将控件添加到 Android 中的选项卡布局?

标签 android android-widget

我正在学习本教程 https://developer.android.com/guide/tutorials/views/hello-tabwidget.html并且已经完成了。现在我实际上想向这些选项卡添加一些控件,例如文本框(文本编辑)。

我该怎么做?我使用 eclipse 作为我的 ide 转到我的 mail.xml 并转到布局 View ,现在我得到一个 NullPointerException,所以我什至不能再将东西拖到布局上。

编辑

这是我的

<?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 最初开始工作有点有趣,因为有很多代码开销,但一旦你完成了工作,它们就不会太糟糕。为了让选项卡正常工作,让我们从改进您的 XML 文件开始,然后我们可以确保您实际加载它们的代码是正确的。

首先,您的 XML 文件。您不应将所有内容直接包含在 main.xml 中,而应使用 include 功能。顾名思义,这使您可以处理一个单独的 xml 文件,然后用一行将其包含在主文件中。这使得 main.xml 文件更易于阅读。因此,我们将修改您上面的文件,使其看起来像这样:

//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>

然后您需要创建 tab1.xml、tab2.xml 等等。这些是普通的 xml 文件,因为它们以包含任意数量的其他小部件的 ViewGroup(即 LinearLayout、RelativeLayout)开头。这些小部件可以是 EditText、按钮、自定义 View 等任何您想要的东西。唯一的规则是父 ViewGroup(顶部的那个)必须有一个唯一的 ID,格式为 android:id="@+id/someUniqueName"。您将使用它来引用代码中的特定布局/选项卡。因此,例如,这将是:

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>

完成后,我们可以查看您的代码。我想您可能已经知道了,但为了以防万一,这就是您想要的:

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);
    }
}

如上所示,您可以将其中一个选项卡的内容设置为单独的 Activity 。在这种情况下,该 Activity 的定义与任何其他 Activity 一样,具有自己的类、布局等。通常您不应该这样做,而只是使用不同的 View (使用 setContent(R.id.tabXLayout),但有时它是必需的。例如,如果您希望其中一个选项卡有一个列表,那么您需要在其中启动一个扩展 ListView 的 Activity ,并包含 ListView 的所有样板代码。

希望对您有所帮助!

关于android - 如何将控件添加到 Android 中的选项卡布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2576661/

相关文章:

android - 无法从 Android 上的 xmpp 服务器获取公共(public)房间列表?

android - 在透明形状上投下阴影?

Android - 动态分配和检索 ID

android - 安卓用户界面的规则

android - Firebase admin sdk 无法解码 jwt token

java - 如何从变量类型 Uri 获取绝对路径

android - Android 在 Equalizer 类中提供的频段范围是多少?

android - 我想在 8 :00 am everyday 显示通知

android - 如何在手机解锁时显示消息

java - 如何计算在 TextView 中看到的字母数量?