Android : Issue positionning customLinear Layout

on Sunday, October 5, 2014


I've created a custom Linear layout in order to have "square" layout fitting the width of its parent but I'm having trouble positioning it with another LinearLayout. I want the LinearLayout to be just below the SquareLinearLayout and I got this (both layout are top aligned):


enter image description here


I've tried android:parentAlignBottom="true" and android:gravity="bottom" but nothing works for me ...


Here is my layout xml :



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.dekajoo.rpg.MainActivity" >

<com.dekajoo.rpg.custom_layouts.SquareLinearLayout
android:id="@+id/game_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="#f00" />

<android:LinearLayout
android:id="@+id/control_bar"
android:layout_width="match_parent"
android:layout_height="100dip"
android:background="#0f0"
android:gravity="center"
android:orientation="horizontal" />

</RelativeLayout>


And here is my SquareLinearLayout class :



package com.dekajoo.rpg.custom_layouts;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class SquareLinearLayout extends LinearLayout {

public SquareLinearLayout(Context context) {
super(context);
}
public SquareLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/*public SquareView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}*/

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size);
}
}


Thanks,


0 comments:

Post a Comment