Leow Zheng Xu
Leow Zheng Xu

Reputation: 86

How do i make my icons spread neatly in linear layout?

There is a gap on my action bar on the right after i put the icons. How do i evenly spread the icons? Here's a screen shot

enter image description here

and my xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:src="@drawable/menu" />

<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/menu" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/menu" />

<ImageButton
    android:id="@+id/imageButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
        android:layout_gravity="right"
    android:src="@drawable/menu" />

</LinearLayout>

Upvotes: 0

Views: 101

Answers (2)

Gajendra Rawat
Gajendra Rawat

Reputation: 3663

Use This

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"

    android:weightSum="1.0" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:src="@drawable/menu"
        android:layout_weight=".25" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu"
         android:layout_weight=".25"  />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu" 
         android:layout_weight=".25" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:layout_gravity="right"
        android:src="@drawable/menu" 
         android:layout_weight=".25" />

    </LinearLayout>

Upvotes: 0

Amulya Khare
Amulya Khare

Reputation: 7698

Use: android:layout_weight="1" for each of them

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal" >

    <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:src="@drawable/menu" android:layout_weight="1"/>

    <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menu" android:layout_weight="1"/>

    <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menu" android:layout_weight="1"/>

    <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="right"
            android:src="@drawable/menu" />

</LinearLayout>

Upvotes: 3

Related Questions