How to align ImageButton to the right in a horizontal LinearLayout

I have ImageButtona LinearLayout. I am trying to put ImageButtonright in horizontal LinearLayout. I use layout_gravity="right", but it does not work. Please help me.

 <LinearLayout
    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:orientation="vertical"
    android:weightSum="5"
    tools:context="com.startsoft.musicalsound.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3"
        android:background="@drawable/img_bg_header"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/btn_menu"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="right"
            android:background="@android:color/transparent"
            android:src="@drawable/ic_action_sort_by_size" />
    </LinearLayout>
+4
source share
2 answers

Use android:gravity="right"in your parent layout ImageButton. You can also remove android:layout_gravity="right"from it.

+1
source

Set the gravity of the button to the right like this: android: layout_gravity = "right"

0
source

Source: https://habr.com/ru/post/1584383/


All Articles