在Android布局中无法将视图转换为ViewGroup(View can not be cast in to ViewGroup in android layout)

编程入门 行业动态 更新时间:2024-10-05 17:19:05
在Android布局中无法将视图转换为ViewGroup(View can not be cast in to ViewGroup in 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>

我在GUI中遇到以下错误

Exception raised during rendering: android.view.View cannot be cast to android.view.ViewGroup

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

I am designing a layout for android application. Here is the skeleton of the layout.

What I tried is that use a table layout since GUI can be broken in to table rows and columns. Basically, I wanted to break the layout in to two columns as you see. Here is the xml code

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

I get following error in the GUI

Exception raised during rendering: android.view.View cannot be cast to android.view.ViewGroup

I think this says we can not group views. If it is not possible, how to deal with this. I am from web background. I thought I can use view as a div like in html.

最满意答案

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

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

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

You can group Views, but you need to use a ViewGroup not a View.

Examples of ViewGroups are LinearLayout (for displaying sub-Views horizontally or vertically), RelativeLayout (for displaying sub-Views using relative positions), FrameLayout, TableLayout etc. Use whichever ViewGroup suits your needs.

To get something working, change the two Views in your XML to FrameLayouts. And also give the Button and TextView layout parameters (e.g. android:layout_width="wrap_content", same for height) - otherwise you'll get another error when you run it.

更多推荐

本文发布于:2023-08-07 18:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   转换为   布局   Android   ViewGroup

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!