在Android的Tabhost中设置选项卡内的多个项目

时间:2013-05-02 08:27:36

标签: android xml android-layout layout

对于我正在处理的应用程序,我希望有3个选项卡,其中包含不同的元素。 我试图找到一种方法来实现这一点,但我似乎找不到我需要的例子。

基本上我想要这个:

标签1,包含4个按钮和一个文本视图

选项卡2,包含可滚动的imageview

标签3,包含webview

我想在不使用活动的情况下解决这个问题,因为所有3个标签都需要访问相同的数据。

目前我使用此xml作为布局:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/photoLayout">
            <Button
                android:text="@string/photoButton"
                android:layout_column="0"
                android:id="@+id/photoButton"
                android:textSize="10dp" />
            <Button
                android:text="@string/locateButton"
                android:layout_column="1"
                android:id="@+id/locateButton"
                android:textSize="10dp" />
            <Button
                android:text="@string/cropButton"
                android:layout_column="2"
                android:id="@+id/cropButton"
                android:textSize="10dp" />
            <Button
                android:text="reset"
                android:layout_column="3"
                android:id="@+id/resetButton"
                android:textSize="10dp" />
            <ScrollView
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="fill_parent"
                android:layout_height="370dp"
                android:id="@+id/scrollView1"
                android:fillViewport="true">
                <TextView
                    android:text="Text"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/textView1" />
            </ScrollView>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/webviewLayout">
            <WebView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/webView1" />
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/imageLayout">
            <ScrollView
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="fill_parent"
                android:layout_height="412dp"
                android:id="@+id/scrollView2"
                android:fillViewport="true">
                <ImageView
                    android:src="@android:drawable/ic_menu_gallery"
                    android:layout_column="0"
                    android:id="@+id/imageView1"
                    android:layout_height="411.6dp"
                    android:layout_width="316.7dp"
                    android:scaleType="centerInside" />
            </ScrollView>
        </LinearLayout>
    </FrameLayout>
</LinearLayout>

这个问题是我得到一个运行时异常说:

Java.Lang.RuntimeException: Binary XML file line #1: You must supply a layout_width attribute.

任何帮助解决此问题的人都将不胜感激!

1 个答案:

答案 0 :(得分:1)

要回答引发的异常,您需要在声明的每个元素中指定layout_width和layout_height,在您的情况下,您没有在这些按钮上指定任何元素。

但是为了表达我的好奇心,我相信你无论如何应该在单独的活动和单独的布局中进行,“相同的数据”总是可以使用我相信的额外内容传递。