android:layout_marginBottom在2.2(Froyo)中似乎无法正常工作

11

我有一个相对布局中包含 ImageView 块:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dip"
    android:layout_marginLeft="81dip"
    android:src="@drawable/swipehelp"
    />

这段代码在 Android 1.6 和高分辨率屏幕上可以按照预期绘制图像,但在2.2版本中似乎忽略了layout_marginBottom属性,总是将图像对齐到底部。是否有人遇到过这个问题,并且知道如何解决?

编辑1:

它位于一个RelativeLayout内,声明如下:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/excusescreen"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/woodbg">

编辑2:

以下是完整的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/excusescreen"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/woodbg">
   <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imlateTopBar"
    android:layout_width="fill_parent"
    android:layout_height="44dip"
    android:background="@drawable/topandbottombars"
    android:layout_alignParentTop="true"    
    >
    <ImageView
            android:id="@+id/excHomeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_marginTop="7dip"
            android:src="@drawable/catexcusehomebtn"
            >
    </ImageView>
    <ImageView
            android:id="@+id/excBackToCatsBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginTop="7dip"
            android:src="@drawable/backtocats"
            >
    </ImageView>
     </LinearLayout>
     <ViewFlipper
    android:id="@+id/excuses"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dip"
     >
     </ViewFlipper>
     <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imlateTopBar"
    android:layout_width="fill_parent"
    android:layout_height="44dip"
    android:background="@drawable/topandbottombars"
    android:layout_alignParentBottom="true" 
    >
    <ImageView
        android:id="@+id/emailItBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="7dip"
        android:src="@drawable/emailit"
        >
    </ImageView>
    <TextView
        android:id="@+id/numExcusesText"
        android:layout_width="100dip"
        android:layout_height="30dip"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="7dip"
        android:textColor="#66ffffff"
        android:gravity="center"
        android:textSize="18dip"
        android:textStyle="bold"
        android:text="1/13"
        >
    </TextView>
    <ImageView
        android:id="@+id/shareItBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="7dip"
        android:src="@drawable/shareit"
        >
    </ImageView>
     </LinearLayout>
     <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="50dip"
    android:layout_marginLeft="81dip"
    android:src="@drawable/swipehelp"
    />

 </RelativeLayout>

编辑3

alt text


ImageView在哪个容器中? - methodin
请查看我的容器编辑。 - LoneWolfPR
它对我来说画得差不多了。可绘制的woodbg的尺寸是多少? - Octavian Helm
它是320x455。这很奇怪。我只是不确定为什么它在1.6中可以正确布局,但在2.2中却不能。 - LoneWolfPR
请参见编辑2以获取完整的布局代码。 - LoneWolfPR
5个回答

14

FrameLayout 中添加一个包含以下内容的 View

android:id="@+id/gap"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_alignParentBottom="true"
现在,为了修复 layout_marginBottom,请使用上述代码:
android:layout_above="@+id/gap"

3

好的,经过大量搜索,我找到了一个链接,该问题实际上可以追溯到09年,但是2010年的评论显示在2.2中alignParentBottom存在许多问题。 http://code.google.com/p/android/issues/detail?id=1394

因此,我通过将整个内容放入FrameLayout中,并将ImageView作为RelativeLayout之外的子级并使用layout_gravity定位来解决了这个问题。这样做可以在所有版本中正常工作。以下是最终代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <RelativeLayout
    android:id="@+id/excusescreen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/woodbg"
    android:clipChildren="true">
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imlateTopBar"
    android:layout_width="fill_parent"
    android:layout_height="44dip"
    android:background="@drawable/topandbottombars"
    android:layout_alignParentTop="true"    
    >
    <ImageView
            android:id="@+id/excHomeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_marginTop="7dip"
            android:src="@drawable/catexcusehomebtn"
            >
    </ImageView>
    <ImageView
            android:id="@+id/excBackToCatsBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginTop="7dip"
            android:src="@drawable/backtocats"
            >
    </ImageView>
</LinearLayout>
<ViewFlipper
    android:id="@+id/excuses"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dip"
>
</ViewFlipper>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imlateTopBar"
    android:layout_width="fill_parent"
    android:layout_height="44dip"
    android:background="@drawable/topandbottombars"
    android:layout_alignParentBottom="true" 
    >
    <ImageView
        android:id="@+id/emailItBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="7dip"
        android:src="@drawable/emailit"
        >
    </ImageView>
    <TextView
        android:id="@+id/numExcusesText"
        android:layout_width="100dip"
        android:layout_height="30dip"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="7dip"
        android:textColor="#66ffffff"
        android:gravity="center"
        android:textSize="18dip"
        android:textStyle="bold"
        android:text="1/13"
        >
    </TextView>
    <ImageView
        android:id="@+id/shareItBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="7dip"
        android:src="@drawable/shareit"
        >
    </ImageView>
    </LinearLayout>
    </RelativeLayout>
    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|bottom"
    android:layout_marginBottom="50dip"
    android:src="@drawable/swipehelp"
    />
    </FrameLayout>

我在FrameLayout中遇到了问题,使用layout_gravity = bottom似乎是使其正常工作的方法,但整个视图(如果它是fill_parent或父级是wrap_content)被裁剪/移动到顶部而不是被调整大小:(,就好像尺寸是在边距之前计算的。 - htafoya

1

对我而言,直到我将最外层的'RelativeLayout`'的 layout_height 更改为 fill_parent,Android 1.6 对我来说也是一样的。

显然当将其设置为 wrap_content 时,它无法正确获取边界。如果没有特殊原因需要 RelativeLayout 的宽度和高度包裹它的内容,我会建议像这样指定。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/excusescreen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/woodbg"
    android:clipChildren="true">
    <!-- all the other stuff -->
</RelativeLayout>

谢谢你的建议。我尝试了,但还是没有成功 :-( - LoneWolfPR
你能发布一下woodbg的可绘制资源吗? - Octavian Helm

1

如果您不想添加额外的视图,可以使用一个InsetDrawable并设置android:insetBottom


1
我们遇到了同样的问题,使用放置在ParentBottom上的LinearLayout,并给它加上padding,在其中放置图像/按钮/视图。顺便提一下,将目标上的margin替换为LinearLayout上的padding也可以起到同样的效果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接