how to align an element to right in horizontal LinearLayout?
Asked Answered
B

3

6

I have a LinearLayout that contains a lot of TextViews and ImageButtons, I want to align some of these elements to right, i had a look at this and this but i can't use their tips as i can't change the orientation and can't make android.gravity:right as i don't want to align all the elements to right, also i can't use nested layouts or but the desired elements into RelativeLayout because that shifts the rest of elements to the left and i want them at the center.

this is my code:

 <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="0.15"
            android:layout_marginTop="10dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:background="@drawable/media_mediabar"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/move_backward"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:background="@android:color/transparent"
                android:clickable="true"
                android:scaleType="centerInside"
                android:src="@drawable/media_button_rewind"
                android:layout_marginLeft="7dp"
                android:layout_marginRight="7dp"
                android:tag="released"/>

           <ImageButton
                android:id="@+id/rmeote_mines"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:background="@android:color/transparent"
                android:clickable="true"
                android:scaleType="centerInside"
                android:src="@drawable/remote_minus" />


          <TextView 
                android:id="@+id/remote_plus_minus"
                android:text="0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp" />
            .
            .
            .<!.. some other elements ..!>
        </LinearLayout>

The desired result:

enter image description here

Burnisher answered 22/12, 2013 at 10:29 Comment(6)
Show the desired result (an image), specifying which elements go to left, to center and to right. Also show the complete layout, so that it can be reworked.Comfy
Could you not just set the gravity of the specific view to right?Hellman
@Klaus66 Done, look at the questionBurnisher
@NicolasTyler No, as this is a horizontal layout not verticalBurnisher
I see: .<!.. some other elements ..!> and I don't see the NAMES of the widgets on the imageComfy
you need to use realtivelayout instead put all your views in relative layout and arrange themAutocracy
A
36

The simplest solution would be using empty views with weights as separators.

<LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">
    <!-- Left button -->
    <Button ...
            ... />
    <View android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1" />
    <!-- Middle button -->
    <Button ...
            ... />
    <View android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1" />
    <!-- Right button -->
    <Button ...
            ... />
</LinearLayout>

The separator views can be made invisible as an optimization, because they don't draw anything and are used only for layout. You can tweak the actual 'layout_weight' values to get the desired layout. Starting from API level 14 you can use instances of Space as separators which will improve performance and readability (there is also a version of Space in the support library).

Allhallowtide answered 22/12, 2013 at 12:19 Comment(2)
This is exactly what I was looking for. Worked like a charmAirlift
Wow, simple solution that saved me tons of dead brain cells. :)Horseback
M
7

For such a complex layout you'd be way better of using RelativeLayout instead.

Mauldin answered 22/12, 2013 at 10:39 Comment(0)
F
1

i can't use nested layouts

Then you can't solve your problem.

Nested layout are the heart of Android layout, to create such complex view that you desire, I think you must use nested layouts.

@Ridcully suggested you to use RelativeLayout, this is a good idea. You can combine it with few linear layouts and you be fine.

I think that RelativeLayout should be your base layout.

Fid answered 22/12, 2013 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.