【问题标题】:Android colored view containing textviews包含文本视图的 Android 彩色视图
【发布时间】:2012-10-30 20:33:30
【问题描述】:

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

这很好用。

现在我的问题是:我想要里面有 2 个文本视图,彼此相邻。所以可能:View、Tablelayout、tablerow、textview、textview、tablerow end、tablelayout end、view end。

但这似乎不起作用。它给了我错误“Java.lang.RuntimeException: Unable to start activity ComponentInfo”和“View can't be cast to 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>
...

【问题讨论】:

    标签: android view viewgroup


    【解决方案1】:

    child-views 不能包含在 View 中。

    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);
    

    【讨论】:

    • 我没有看到您编写的 XML 的变化?将视图更改为视图组,我仍然可以更改视图的背景色(现在是视图组)吗?还是我需要在视图组内更改表格布局的颜色?
    • 是的,ViewGroup 具有可以更改的背景,就像您为 View 更改它一样
    • 我已经编辑了我的答案。是的,您将能够在此更改后更改ViewGroup 的背景颜色。希望这会有所帮助。
    • 仍然给出“Java.lang.runtimeexception”。这次是“android.view.inflateexception”,“error inflating class viewgroup”。在 XML 行
    • 我提到了ViewGroup,以便正确理解应该如何使用ViewsViewGroups。对于您的代码,我认为任何基本布局都应该可以正常工作。我已更新我的答案以使用Linear Layout。您也可以将android:layout_height="20dp" 更改为 `android:layout_height="wrap_content"' 并尝试。
    【解决方案2】:

    您在View 标记内放置了多个元素。 View's don't hold children Views,有 ViewGroup 类。请改用FrameLayout,LinearLayout,RelativeLayout 之一。

    【讨论】:

      【解决方案3】:

      不要使用View。将您的 View 更改为 ViewGroup。 View 和 ViewGroup 的区别在于 ViewGroup 可以包含其他 View。如果你想在里面放一些TextViews和其他东西,你应该使用ViewGroup或某种布局,如LinearLayoutRelativeLayout

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-11
        • 1970-01-01
        相关资源
        最近更新 更多