除非我在布局中放置某些内容,否则布局中的背景不会出现

时间:2014-10-11 18:52:32

标签: android android-layout background

我正在尝试在Android中的两个TextView对象周围绘制一个框。在此之前,我在LinearLayout中有两个TextView,但我正在努力展平我的视图层次结构以提高性能。当他们在LinearLayout中时,背景显示为所需,但是当我创建具有相同尺寸的空布局时,我看不到背景显示。
我猜这是因为FrameLayout中没有任何东西(我也试过一个空的LinearLayout)。我也尝试在运行时设置此值,但结果相同。

这在Eclipse的布局编辑器中工作正常,但在我的平板电脑上运行时,背景就不存在了。

任何有关完成相同事情的建议或任何我缺少的建议都会有所帮助。形状如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#508E8F8E" />

    <stroke
        android:width="1dip"
        android:color="#8E8F8E" />

    <corners android:radius="4dp" />

</shape>

这是布局:

<FrameLayout
    android:id="@+id/box_around_text"
    android:layout_width="35dp"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:background="@drawable/background_gray"/>

<TextView
    android:id="@+id/first_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="first_text" />

<TextView
    android:id="@+id/second_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/first_text"
    android:text="second_text" />

1 个答案:

答案 0 :(得分:0)

问题是形状必须在某处定义真实尺寸。如果我在形状本身中定义尺寸或者在布局中使用编号尺寸,我可以显示它。如果我使用了&#39; wrap_content&#39;它就永远不会显示出来。或者&#39; match_parent&#39;选项。