Vertical LinearLayout gravity not working as spected

1.2k Views Asked by At

Im trying to set gravity="bottom" on a LinearLayout, so its children Views stack at the bottom.

The problem is that when I check it on Eclipse it seems fine, but when I run in on the AVD it doesn't work. It just works as if gravity was set to top.

Here is my code:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp" >

<com.pepotegames.spotthedifferences.SquareImageView
    android:id="@+id/picture"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="bottom"
    android:weightSum="4" >

    <ImageView
        android:id="@+id/stars"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:src="@drawable/stars"
        android:background="#55000000"
        android:adjustViewBounds="true" />

</LinearLayout>

This is how it looks in Eclipse, and how it should work:

https://i.stack.imgur.com/9ojaM.png

EDIT: btw my SquareImageView its just an extended ImageView. The problem has nothing to do with it, I've already tested the same layout with a normal ImageView and its the same results.

In case you wanna check it, here it is:

public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
    super(context);
}

public SquareImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
}
}
2

There are 2 best solutions below

0
On BEST ANSWER

I couldn't solve it that way, so this is what I ended up doing

<com.pepotegames.spotthedifferences.SquareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="7dp" >

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_container_rounded" >

    <com.pepotegames.spotthedifferences.SquareImageView
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

    <com.pepotegames.spotthedifferences.QuarterImageView
        android:id="@+id/stars"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:background="@color/trans_mask_mid" />

</FrameLayout>

I made a square root layout, then I used another custom view, an extended ImageView that gets resized to a quarter of its parent height

public class QuarterImageView extends ImageView {
public QuarterImageView(Context context) {
    super(context);
}

public QuarterImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public QuarterImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(),getMeasuredHeight()/4);
}}
1
On
  • try to use relative layout instead of linear layout

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="4" >
    
    <ImageView
        android:id="@+id/stars"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="true"
        android:background="#55000000"
        android:src="@drawable/ic_launcher" /></RelativeLayout>