【问题标题】:ViewFlipper width is wrapping its content and fill_parent is not workingViewFlipper 宽度正在包装其内容,而 fill_parent 不起作用
【发布时间】:2011-12-08 07:44:10
【问题描述】:

这是我的布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="true">
    <TableRow
      android:background="#FF0000">
      <ViewFlipper
        android:id="@+id/viewer_something"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
          android:id="@+id/view1"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#CCCCCC">
          <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button one" />
        </LinearLayout>
        <LinearLayout
          android:id="@+id/view2"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#FFFFFF">
          <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button two" />
        </LinearLayout>
      </ViewFlipper>
        </TableRow>
        <TableRow
          android:background="#FF0000">
          <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button three" />
        </TableRow>
      </TableLayout>
    </LinearLayout>

简而言之,我在 TableRow 中有一个 ViewFlipper。 ViewFlipper 正在缩小到其内容的大小,即使所有元素都指定了 fill_parent。在下面的屏幕截图中,ViewFlipper 具有灰色背景,而 TableRows 具有红色背景。

【问题讨论】:

    标签: android xml layout viewflipper fill-parent


    【解决方案1】:

    在 ViewFlipper 上设置 android:layout_weight="1" 会起作用。

    【讨论】:

    • 是的,确实如此。我不明白为什么它有效,但它有效。你能解释一下吗?
    • 假设您有一个带有各种子视图的父视图。如果将每个子视图设置为 wrap_content 然后指定布局权重,则每个视图将按照其权重占总权重的比例扩展填充剩余的空白区域。因此,如果您在父视图 (LinearLayout) 中只有一个视图 (ViewFlipper),则设置布局权重将导致其填充剩余的空白区域。这就是布局权重起作用的原因。至于为什么宽度上的fill_parent一开始就不起作用,我不太确定,但这可能是测量尚未绘制的视图的问题。
    • 谢谢。仍然对为什么 fill_parent 一开始就不起作用感到困惑——就像你说的那样——但我现在明白为什么添加 layout_weight 会增加额外的保险来填充所有内容。
    猜你喜欢
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 2013-08-04
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    相关资源
    最近更新 更多