Jak wyrównać widget w prawo w poziomym układzie liniowym Androida?

205

Oto kod, którego używam i nie działa:

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

    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:gravity="right">
    </TextView>

</LinearLayout>
awareeye
źródło

Odpowiedzi:

418

Spróbuj dodać pusty Viewwewnątrz poziomej LinearLayoutprzed elementem, który chcesz zobaczyć po prawej stronie, np .:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />                

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
alcsan
źródło
11
To działa! Ale każdy może wyjaśnić, dlaczego? Nie mogłem całkowicie zrozumieć.
GMsoF,
28
Pusty widok próbuje zająć maksymalną ilość miejsca, pozostawiając miejsce dla przycisku.
alcsan
14
Do tej pory była to jedyna rzecz, która działa dla mnie, próbując mieć kilka przycisków po lewej stronie i jeden ostatni przycisk po prawej stronie. Ale wydaje mi się, że to hack. Czy istnieje „właściwy” sposób, aby to zrobić?
IcedDante
8
To jest niesamowite! Ale dlaczego grawitacja = „prawo” nie działa w ten sposób od samego początku? Dlaczego potrzebuje pomocy z tego innego punktu widzenia? A jak to jest „właściwe”, jeśli nie jest, bez dodatkowego widoku?
afrish
1
Nie działa to w układzie podobnym do siatki - nie ma pustej przestrzeni. Rozwiązanie Sherif elKhatib działa.
cdonner
119

Nie zmieniaj grawitacji LinearLayout na „prawo”, jeśli nie chcesz, aby wszystko było po prawej stronie.

Próbować:

  1. Zmień szerokość TextView nafill_parent
  2. Zmień grawitację TextView naright

Kod:

    <TextView 
              android:text="TextView" 
              android:id="@+id/textView1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"   
              android:gravity="right">
    </TextView>
Sherif elKhatib
źródło
1
dzięki za to - to była szerokość fill_parent, która mnie wyrzucała
dldnh
2
Czy układ liniowy został w tym celu zaprojektowany? Czy nie należy po prostu użyć względnego układu, aby to osiągnąć? Czy to rozwiązanie nie jest włamaniem?
user2635088,
to nie zadziała, jeśli masz wiele kontrolek TextView w LinearLayout. Zobacz odpowiedź od @alcsan
Felix
To powinna być zaakceptowana odpowiedź. To podejście nie wykorzystuje żadnych dodatkowych widoków, a co ważniejsze, nie używa układu_waga, który znacznie zmniejsza wydajność.
Kazanie
android:layout_weight = "1"obok siebie android:gravity="right", powinien załatwić sprawę.
Vassilis
63

Jako dodatek do odpowiedzi alcsana możesz używać Spaceod API 14 (Android 4.0 ICE_CREAM_SANDWICH), udokumentuj tutaj .

Space to lekka podklasa View, która może być używana do tworzenia przerw między komponentami w układach ogólnego przeznaczenia.

<?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" >

    <Space
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:text="TextView"
        android:gravity="right" />

</LinearLayout>

W przypadku aplikacji obsługujących poziomy API poniżej 14 dostępna jest android.support.v4.widget.Spacebiblioteka pomocy technicznej Android w wersji 22.1.0.

Weekend
źródło
Ważne jest, że musisz ustawić layout_weight = "1"
code4j
Działa świetnie! Zachowuje szerokość każdego elementu.
phatmann
To działa. Wow, brzydota Androida nigdy nie przestaje mnie zadziwiać
Chris Neve
31

Z układem liniowym

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/select_car_book_tabbar"
        android:gravity="right" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/my_booking_icon" />
    </LinearLayout>

wprowadź opis zdjęcia tutaj

z FrameLayout

<FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/select_car_book_tabbar">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|right"
            android:src="@drawable/my_booking_icon" />
    </FrameLayout>

z RelativeLayout

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/select_car_book_tabbar">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:src="@drawable/my_booking_icon" />
    </RelativeLayout>
DeltaCap019
źródło
21

ustawienie widoków layout_weight="1"załatwi sprawę.!

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" />

<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Saad Mahmud
źródło
2
Takie proste rozwiązanie +1
Alex
1
Jeśli ktoś wciąż szuka, jest to właściwe rozwiązanie :)
passatgt
To jest magia! Jak się tego dowiedziałeś?
andreikashin
@ andreikashin, Zastanów się, czy to pomogło. Dzięki.
Saad Mahmud
13

Dodaj android:gravity="right"do LinearLayout. Zakładając, że TextViewmalayout_width="wrap_content"

Ronnie
źródło
2
W szczególności powiedział, aby wyrównać pojedynczy element w LinearLayout. Nie sam układ liniowy: sam układ jest ustawiony na fill_parent.
RichieHH,
8

po prostu dodaj android:gravity="right"swój układ liniowy.

Idrees Ashraf
źródło
4

Zrobiłem to w najprostszy sposób:

Wystarczy wziąć jeden RelativeLayout i umieścić w nim widok dziecka , który chcesz umieścić po prawej stronie.

    <LinearLayout
        android:id="@+id/llMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f5f4f4"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="20dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingTop="20dp">

        <ImageView
            android:id="@+id/ivOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />


        <TextView
            android:id="@+id/txtOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="Hiren"
            android:textAppearance="@android:style/TextAppearance.Medium"
            android:textColor="@android:color/black" />

        <RelativeLayout
            android:id="@+id/rlRight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="right">


            <ImageView
                android:id="@+id/ivRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:src="@drawable/ic_launcher" />

        </RelativeLayout>
    </LinearLayout>

Mam nadzieję, że ci to pomoże.

Hiren Patel
źródło
3

Powinieneś użyć RelativeLayout i przeciągnąć je, aż będzie dobrze :)

    <ImageView
        android:id="@+id/button_info"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="10dp"
        android:contentDescription="@string/pizza"
        android:src="@drawable/header_info_button" />

</RelativeLayout>
detman
źródło
2
„przeciągnij je” nie jest zalecane w przypadku ekranu o wielu rozdzielczościach
Moses Aprico
3

linear layoutza pomocą, layout_width="fill_parent"a także widżetu z tym samym layout width+ gravity as rightwyrównałby go do prawej.

Korzystam z 2 TextViews w poniższym przykładzie, topicTitlepo lewej i topicQuestionspo prawej stronie.

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:orientation="horizontal">

    <TextView
        android:id="@+id/topicTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/topicQuestions"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textSize="18sp"
        android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>

Wynik

prayagupd
źródło
2

Spróbuj zmienić layout_width na, android:layout_width="match_parent"ponieważ gravity:"right"wyrównuje tekst wewnątrz layout_width, a jeśli wybierzesz zawijanie treści, nie będzie ona miała dokąd pójść, ale jeśli wybierzesz element nadrzędny dopasowujący, może przejść w prawo.

mrvinent
źródło
2

Nie musisz używać żadnego dodatkowego widoku ani elementu:

// to takie proste i proste

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
>

// to pozostało wyrównanie

<TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="No. of Travellers"
      android:textColor="#000000"
      android:layout_weight="1"
      android:textStyle="bold"
      android:textAlignment="textStart"
      android:gravity="start" />

// to jest prawidłowe wyrównanie

<TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Done"
      android:textStyle="bold"
      android:textColor="@color/colorPrimary"
      android:layout_weight="1"
      android:textAlignment="textEnd"
      android:gravity="end" />

</LinearLayout>
kamaljeet Singh
źródło
0

W przypadku TextView:

<TextView 
    android:text="TextView" 
    android:id="@+id/textView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"   
    android:gravity="right"
    android:textAlignment="gravity">    
</TextView>
Роман Елизаров
źródło
0

Dodawanie widoku jest nieco trudne i obejmuje całą szerokość ekranu w następujący sposób:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />                

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Spróbuj tego kodu:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create Account"/>

</LinearLayout>
Zafar Iqbal
źródło
-1

Oto próbka. klucz do uzgodnienia jest następujący

android:layout_width="0dp"
android:layout_weight="1"

Pełny kod

<?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="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp">

    <TextView
        android:id="@+id/categoryName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="abcd" />

    <TextView
        android:id="@+id/spareName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="efgh" />

</LinearLayout>

wprowadź opis zdjęcia tutaj

Rohit Mandiwal
źródło
-1

Użyj match_parent i grawitacji, aby ustawić tekst TextView w prawo, w następujący sposób:

<?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">

    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">
    </TextView>

</LinearLayout>
Guillermo Gonzalez
źródło
-2

Spróbuj tego..

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



    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>

</LinearLayout>
Shubham
źródło