0

我制作了一个程序,它在窗口顶部(标题栏正下方)有一个视图。根据程序中发生的情况,视图的颜色会发生变化。

这工作得很好。

现在我的问题是:我想要里面有 2 个文本视图,彼此相邻。所以可能:视图、表格布局、表格行、文本视图、文本视图、表格行结束、表格布局结束、视图结束。

但这似乎不起作用。它给了我错误“Java.lang.RuntimeException:无法启动活动 ComponentInfo”和“无法将视图转换为视图组”。

在 java 代码中,我没有触及 xml 代码中的任何新视图,唯一触及 XML 的 java 是TopView = (View)findViewById(R.id.TopView);TopView.setBackgroundColor(Color.GREEN );Topview 是外部视图,并且在其中没有任何东西的情况下可以完美地工作。

这是 XML 代码

    ...
<View
        android:id="@+id/TopView"
        android:layout_width="fill_parent"
        android:layout_height="20dp" 
        android:background="#8E8E8E" >



        <TableLayout
                android:id="@+id/tableTrolo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TableRow
                    android:id="@+id/TableRow000"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:gravity="left"
                    android:orientation="vertical" >


            <TextView
                        android:id="@+id/lbl1"
                        android:layout_width="120dp"
                        android:layout_height="wrap_content"
                        android:text="Vare :"
                        android:textSize="22dp" />

                    <TextView
                        android:id="@+id/lbl2"
                        android:layout_width="180dp"
                        android:layout_height="wrap_content"
                        android:text="du grim" />

            </TableRow>
            </TableLayout>


    </View>
...
4

3 回答 3

1

View您在标签内放置多个元素。View的不要持有孩子的意见,有ViewGroup课。请改用FrameLayout, LinearLayout,之一RelativeLayout

于 2012-10-30T12:04:56.607 回答
1

不要使用View. 将您的更改ViewViewGroup. View 和 ViewGroup 的区别在于 ViewGroup 可以包含其他 View。如果你想在里面放一些 TextView 和其他东西,你应该使用ViewGroupor 某种布局,比如LinearLayoutor RelativeLayout

于 2012-10-30T12:05:12.933 回答
1

你不能有child-views里面的View

AView始终布局层次结构中的叶节点。

1.将您的xml布局的第一行重写为:

<LinearLayout
    android:id="@+id/TopView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:background="#8E8E8E">

2.修改对应的Java代码如下:

TopView = (LinearLayout)findViewById(R.id.TopView);
于 2012-10-30T12:06:05.670 回答