0

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());
}
}
Juan Bentel
  • 477
  • 2
  • 5
  • 15
  • is FrameLayout necessarily for your layout, if now you should switch to RelativeLayout for better alignments. – moh.sukhni Sep 06 '13 at 21:39
  • Actually it is, because I need the stars to render above the image. Here you can see an example of the final look: http://puu.sh/4kKq9.jpg This Layout is used inside a GridView – Juan Bentel Sep 06 '13 at 21:44

2 Answers2

0
  • 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>
    
moh.sukhni
  • 2,220
  • 19
  • 24
  • I think that you can't use weight inside a RelativeLayout. Eclipse is pointing out "Invalid layout param in a RelativeLayout: layout_weight. I could use a RelativeLayout if I wasn't trying that the stars take 1/4 of the image, but this is not the case. – Juan Bentel Sep 06 '13 at 22:02
0

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);
}}
Juan Bentel
  • 477
  • 2
  • 5
  • 15