Android:为什么View背景在编辑器中绘制而在应用程序中绘制?

时间:2013-11-17 09:46:20

标签: java android view

我有一个带有xml文件的Android项目,该文件代表适配器的行。

此行左侧有彩色视图。

出于某种原因,View在Eclipse中的图形编辑器中正确呈现,但在设备上运行时却没有。

View以正确的大小绘制,但没有像编辑器那样的蓝色背景。


我注意到当我转储UI层次结构时,View节点不在层次结构中。

如果我打破绑定适配器中的视图,我可以看到mMeasuredHeight是正确的但mMeasuredWidth是0.我觉得我缺少一些基本的东西。


附件是截图和xml文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <View
        android:id="@+id/view_header_accent"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/blue_primary" />

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="@dimen/grid_small"
        android:layout_marginTop="@dimen/grid_small"
        android:layout_toLeftOf="@+id/img_cancel"
        android:layout_toRightOf="@+id/view_header_accent"
        android:text="Brad"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txt_subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txt_title"
        android:layout_alignRight="@+id/txt_title"
        android:layout_below="@+id/txt_title"
        android:paddingBottom="@dimen/grid_small"
        android:text="Active"
        android:textColor="#80000000"
        android:textSize="14sp" />

    <ImageView
        android:id="@+id/img_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="@dimen/grid_small"
        android:src="@drawable/ic_cancel" />

</RelativeLayout>

Eclipse中图形编辑器的截图:

Screenshot from editor

设备屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

发现了这个问题。

问题是因为顶级视图是一个RelativeLayout,其中wrap_content作为高度。

将此与具有match_height为match_parent的子项相结合,并且彩色子视图的测量高度最终计算为0.

不幸的是,解决方法是为行设置固定高度,或者可选地指定彩色视图的顶部和底部对齐以匹配其他子视图。

最终的视图如下所示:

<View
    android:id="@+id/view_header_accent"
    android:layout_width="8dp"
    android:layout_height="match_parent"
    android:layout_alignBottom="@+id/txt_subtitle"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@+id/txt_title"
    android:background="@color/blue_primary" />