I have spent hours trying to work this out, and spent a long time looking through other answers.
I’m evidently missing something simple, but really have been trying.
I cut my code down to the very basics to find the problem.
A layout has a backround, the top half of which is nice graphics, below the top half I am hoping to have the buttons.
The background is on the top, first linearlayout ( vertical ).
Then, inside this, are two vertical linear layouts.
The top vertical layout, has a weight of 1.
The bottom vertical layout, has a weight of 1.
This means to me, that the top layout should take up HALF of the screen. The buttons then going in the second layout should be ok to take up the rest of the screen.
However, when I start adding to the second layout, it no longer takes up half of the screen, but starts taking up space on the top half.
All I want to do, is to add four imagebuttons to the bottom half of the screen, centralised horizontally.
here is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/menubackground"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Layout 1, the top half -->
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<!-- Layout 2, the bottom half, containing buttons -->
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:src="@drawable/playbtnimg" android:layout_weight="1"/>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:src="@drawable/achievementsbtnimg" android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
Those two buttons, are now starting to take up around 2/3rd of the vertical space. They should not be going into the top half of the screen.
I’m trying to use layouts in this way so it will work on different screen sizes.
I would like the images to scale to fit within their linear layout cell, and not be cut off at the top of sides.
Any help is much appreciated.
Add a
weightsumattribute to your root element ie the parentLinearLayouthere is what it would look like
same would be for the
LinearLayouthaving buttonsThis would help further you can read more in android documentation