我制作了一个程序,在窗口顶部(标题栏下方)有一个视图。根据程序中的内容,视图的颜色将发生变化。
这很好用。
现在我的问题是:我想要两个文本视图在里面,一个挨着一个。所以很可能:视图,表布局,表行,文本视图,文本视图、表行结束,表布局结束,视图结束。
但这似乎并不奏效。它给了我错误"Java.lang.RuntimeException:无法启动活动ComponentInfo"one_answers"视图无法转换为viewgroup"。
在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>
...
View
中不能有child-views
。
View
始终是布局层次结构中的叶节点。
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);
您在View
标记中放置了多个元素。View
不包含子视图,有ViewGroup
类。请改用FrameLayout
、LinearLayout
和RelativeLayout
中的一个。
不要使用View
。将您的View
更改为ViewGroup
。视图和视图组之间的区别在于,视图组可以包含其他视图。如果你想在里面放一些TextViews和其他东西,你应该使用ViewGroup
或某种布局,比如LinearLayout
或RelativeLayout
。