简体   繁体   中英

android: layout_marginBottom doesn't seem to work properly in 2.2 (Froyo)

I have this ImageView block inside a Relative layout:

<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"
    />

This draws the image right where it would be expected in both normal and high density resolution screens on Android 1.6 however on 2.2 it seems to ignore the layout_marginBottom and always draw the image aligned all the way at the bottom. Has anyone seen this before, and if so do you know a fix?

Edit 1:

It sits inside a RelativeLayout declared thusly:

<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">

Edit 2:

Here's the full layout code:

<?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>

Edit 3

替代文字

In the FrameLayout add a View with the following:

android:id="@+id/gap"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_alignParentBottom="true"

Now, in order to fix the layout_marginBottom , use the above:

android:layout_above="@+id/gap"

Ok, so after a lot of searching I found a link to this Issue that actually dates back to 09 but the comments from 2010 show lots of problems with alignParentBottom in 2.2. http://code.google.com/p/android/issues/detail?id=1394

So, I did a workaround by putting the whole thing in a FrameLayout and made the ImageView a child of that outside my RelativeLayout and placed with the layout_gravity. That did the trick and made it work in all versions. Here's the final code:

<?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>

如果您不想添加其他视图,则可以将InsetDrawableandroid:insetBottom一起使用。

We had the same problem and used a LinearLayout positioned with ParentBottom, giving that a padding, and putting the image/button/view inside it. Using a margin on the target instead of a padding on the LinearLayout works as well btw.

It was the same for Android 1.6 for me until I changed the layout_height to fill_parent on the outermost 'RelativeLayout`

Apparently it can't get the bounds correct when settings it to wrap_content . If there is no special reason for having the RelativeLayout s width and height to wrap to it's content I'd say specify it like that.

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM