【问题标题】:How to set height of one layout component to the same height of another component?如何将一个布局组件的高度设置为另一个组件的相同高度?
【发布时间】:2011-06-08 18:09:27
【问题描述】:

如何将一个布局组件的高度设置为与另一个组件相同: 例子: 我可以设置 android:layout_height="@+id/info_box.height" 或做类似的事情吗?

我希望 ImageView 的高度与我的 LinearLayout 的高度相匹配

ImageView android:id="@+id/border"
  android:src="@drawable/frame"
android:layout_below="@+id/title"
android:layout_width="fill_parent"
android:layout_height="????"    
android:scaleType="fitXY" 
android:drawingCacheQuality="auto"
/>

<LinearLayout
android:id="@+id/info_box"
android:orientation="vertical"
android:layout_below="@+id/title"
android:background="@layout/my_bg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
... other stuff . .
 </LinearLayout>

【问题讨论】:

  • 嗯。你为什么不把你的 imagview 放在你的线性布局中并将高度设置为 fill_parent?
  • @Falmarri:如果他的 ImageView 和 LinearLayout 在水平 LinearLayout (未指定)内,则不会有相同的结果。另外,您可以为孩子设置 fill_parent 并为父母设置 wrap_content 吗?这看起来几乎是自相矛盾的。
  • 需要更多信息。您的 ImageView 和 LinerLayout 所在的根 ViewGroup 是什么?您想通过布局实现什么目标?
  • 这是一个简单的问题,我可以将一个组件的高度设置为等于另一个组件,即使它们不在同一个容器中。我不需要其他布局组件的方式。
  • 那么那个特定的答案是否定的。按照您的建议,没有直接的方法可以做到这一点。如果您希望获得其他建议以实现类似结果,请发布更多信息。

标签: android height android-layout


【解决方案1】:

实现这一点的唯一方法是让两个视图成为同一个容器的一部分。 LinearLayout 或 RelativeLayout 通常非常适合这种情况。也可以通过代码实现。

【讨论】:

  • 如果它们是同一个容器的一部分,你会怎么做?我认为他或她想让 ImageView 缩放到 LinearLayout 的高度。
  • 没关系,我找到了。在图像视图上:scaletype="centercrop"、layout_height="fill_parent" 和 layout_width="fill_parent"。钥匙? scaletype="centercrop" 在布局编辑器上不起作用。
【解决方案2】:

您可以使用android:layout_weight

将具有 fill_parent 和相同值 layout_weight 的两个视图(border 和 info_box)放入具有orientation=verticale 的相同LinearLayout 中。

使用这种技术,边框和 info_box 将具有相同的高度。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/border"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/title"
        android:layout_weight="2"
        android:drawingCacheQuality="auto"
        android:scaleType="fitXY"
        android:src="@drawable/frame" />

    <LinearLayout
        android:id="@+id/info_box"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/title"
        android:layout_weight="2"
        android:background="@layout/my_bg"
        android:orientation="vertical" >

        ... other stuff . .

    </LinearLayout>

</LinearLayout>

【讨论】:

  • 他不希望所有视图都带有 fill_parent。他希望一个是 wrap_content,另一个和第一个一样高。
  • 他可以改变第一个LinearLayout的宽度和高度,并选择他想要的大小。但是 ImageView 和第二个 LinearLayout 将始终具有相同的高度(或宽度),因为它们具有相同的 layout_weight..
猜你喜欢
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
相关资源
最近更新 更多