【问题标题】:Android layout_height doesn't wrap_contentAndroid layout_height 不 wrap_content
【发布时间】:2012-09-05 10:03:18
【问题描述】:

我有一个像这样的 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" >
    <RelativeLayout
        android:id="@+id/toppane"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true" >
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/bottompane"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/genericbutton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true" />
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_above="@id/genericbutton" />
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_toRightOf="@id/genericbutton"
            android:layout_alignTop="@id/genericbutton" />
    </RelativeLayout>   
</RelativeLayout>

我希望底部窗格在宽度和高度上都包含其内容。

底部窗格上的 Wrap_content 适用于宽度(总共 100dp),但 wrap_content 似乎不适用于高度。 bottompane 获取父级的高度,因此 toppane 甚至不可见。

为什么会这样?我错过了什么或做错了什么?

编辑:图片很快就会出现。我想要实现的是底部面板具有简单地包装其内容的高度。所以不要高于彼此上方的两个按钮(100dp),顶部面板填充底部面板上方的其余空间,一直到它与父级对齐。

EDIT2:image http://img405.imageshack.us/img405/9871/wrapcontent.png 蓝线是布局,尽管高度设置为 wrap_content。红线是应该的(只是包装内容)。绿线是toppane的布局方式(粘在bottompane的顶部)

【问题讨论】:

  • 你想达到什么效果请加图
  • 添加图片及问题说明

标签: android layout


【解决方案1】:

只需用这个替换你的buttons 位置和关系

<Button
            android:id="@+id/firstButton"
            android:layout_width="50dp"
            android:layout_height="50dp"/>

        <Button
            android:id="@+id/genericbutton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@+id/firstButton" />

        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@+id/firstButton"
            android:layout_toRightOf="@id/genericbutton" />

【讨论】:

    【解决方案2】:

    您不能在 RelativeLayout 中拥有具有 alignParenBottom 和 wrap_content 的孩子。 解释是here

    一种可能的解决方案是嵌套 LinearLayouts

    <RelativeLayout
        android:id="@+id/toppane"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottompane"
        android:layout_alignParentTop="true" >
    </RelativeLayout>
    
    <LinearLayout
        android:id="@+id/bottompane"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <Button
                android:layout_width="50dp"
                android:layout_height="50dp" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <Button
                android:layout_width="50dp"
                android:layout_height="50dp" />
    
            <Button
                android:layout_width="50dp"
                android:layout_height="50dp" />
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      尝试用垂直线性布局替换您的父相对布局。

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >
          <RelativeLayout
              android:id="@+id/toppane"
              android:layout_width="match_parent"
              android:layout_height="50dp"
              android:layout_alignParentTop="true" >
          </RelativeLayout>
          <RelativeLayout
              android:id="@+id/bottompane"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true">
              <Button
                  android:id="@+id/genericbutton"
                  android:layout_width="50dp"
                  android:layout_height="50dp"
                  android:layout_alignParentBottom="true" />
              <Button
                  android:layout_width="50dp"
                  android:layout_height="50dp"
                  android:layout_above="@id/genericbutton" />
              <Button
                  android:layout_width="50dp"
                  android:layout_height="50dp"
                  android:layout_toRightOf="@id/genericbutton"
                  android:layout_alignTop="@id/genericbutton" />
          </RelativeLayout>   
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 2011-08-14
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-31
        • 1970-01-01
        • 1970-01-01
        • 2019-11-12
        相关资源
        最近更新 更多