LinearLayout中的子宽度

时间:2013-01-30 19:30:52

标签: android android-linearlayout android-ui

我有一个LinearLayout用于制作一个显示日期的框:

<LinearLayout android:id="@+id/date_layout"
    android:background="@drawable/bg_frame"
    android:layout_gravity="center"
    android:layout_marginRight="3dp"
    android:layout_rowSpan="3"
    android:minWidth="65dp"
    android:orientation="vertical"
>
    <TextView android:id="@+id/date"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textColor="@android:color/holo_blue_bright"
        android:textSize="20sp"
    />
    <View
        android:background="@android:color/holo_blue_bright"
        android:layout_height="1dp"
        android:layout_width="match_parent"
    />
    <TextView android:id="@+id/day_of_week"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:textColor="@android:color/holo_blue_bright"
    />
</LinearLayout>

它创建了一个漂亮的小方框(上面代码中没有View):Date Box

当我将View放在那里时,它会将整个框架拉伸到整个屏幕上,我只是想在框的宽度上画一条线而不改变它的宽度,为什么它不起作用我有它编码? android:layout_width="match_parent"不应该只匹配包含LinearLayout的宽度吗?为什么它适用于两个TextViews而不是View

LinearLayout包含GridLayout

我还发现,如果我将View替换为TextView,但保留所有其他内容完全相同,则会按照我的要求运行,并且不会扩展以填充整个屏幕宽度。为什么TextView的工作方式和预期的一样?

1 个答案:

答案 0 :(得分:0)

您在LinearLayout上没有layout_width或layout_height。因此它的宽度和高度未定义,可能默认为全屏。您需要将其宽度设置为wrap_content。

相关问题