在Android布局中无法将视图转换为ViewGroup

new*_*day 4 android android-layout

我正在为Android应用程序设计一个布局.这是布局的骨架. 在此输入图像描述

我尝试的是使用表格布局,因为GUI可以分成表格行和列.基本上,我想把布局分成两列,如你所见.这是xml代码

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <!-- 2 columns -->
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <View
            android:id="@+id/view1"
            android:layout_weight="7"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/textView1"
                android:text="Column 1"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </View>

        <View
            android:id="@+id/view2"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="wrap_content" >
            <Button
                android:id="@+id/button1"
                android:text="Column 2" />
        </View>

    </TableRow>


</TableLayout>
Run Code Online (Sandbox Code Playgroud)

我在GUI中遇到以下错误

Exception raised during rendering: android.view.View cannot be cast to android.view.ViewGroup
Run Code Online (Sandbox Code Playgroud)

我认为这表示我们无法对观点进行分组.如果不可能,该如何处理.我来自网络背景.我以为我可以将视图用作html中的div.

nmw*_*nmw 8

您可以对视图进行分组,但需要使用ViewGroup而不是View.

ViewGroups的示例是LinearLayout(用于水平或垂直显示子视图),RelativeLayout(用于显示使用相对位置的子视图),FrameLayout,TableLayout等.使用适合您需要的ViewGroup.

要使某些工作正常,请将XML中的两个视图更改为FrameLayouts.并且还提供Button和TextView布局参数(例如android:layout_width="wrap_content",相同的高度) - 否则在运行时会出现另一个错误.