Подтвердить что ты не робот

CSS "float: right" эквивалент свойства в LinearLayout на Android?

В CSS мы можем написать:

<div style="float:right"> Text1 </div>
<div style="float:right"> Text2 </div>

таким образом Text1 появится справа.

Я пытаюсь сделать то же самое с LinearLayout, представление должно появляться справа налево:

<LinearLayout android:id="@+id/linearLayout1" android:layout_gravity="right" android:gravity="right"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="1" android:weightSum="2" android:orientation="horizontal">
        <!-- First Column should be on the right : Text1-->
        <LinearLayout android:id="@+id/linearLayout2"
            android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:gravity="right"
            android:layout_weight="1">...</LinearLayout>
        <!-- Second Column should be on the left : Text2 -->
        <LinearLayout android:id="@+id/linearLayout3"
            android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:gravity="right"
            android:layout_weight="1">...</LinearLayout>
</LinearLayout>

Спасибо

4b9b3361

Ответ 1

Это может быть

<LinearLayout android:id="@+id/linearLayout1"
    android:gravity="right"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:weightSum="2"
    android:orientation="horizontal"
    android:layout_gravity="right"
    >
    <!-- Second Column should be on the left : Text2 -->
    <LinearLayout android:id="@+id/linearLayout3"
        android:layout_width="wrap_content" android:layout_height="fill_parent" 
        android:layout_weight="1">...</LinearLayout>
    <!-- First Column should be on the right : Text1-->
    <LinearLayout android:id="@+id/linearLayout2"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1">...</LinearLayout>

Ответ 2

Не знаю, возможно ли это с помощью LinearLayout, но вы можете достичь того, что вам нужно, с RelativeLayout следующим образом:

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

    <LinearLayout
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Text1"
            android:textAppearance="@android:style/TextAppearance.Large" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/text1"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Text1"
            android:textAppearance="@android:style/TextAppearance.Large" />
    </LinearLayout>

</RelativeLayout>

В относительной компоновке вы можете выровнять контейнер макета text1 с правой стороной относительно родителя (android: layout_alignParentEnd = "true" или android: layout_alignParentRight = "true" в зависимости от совместимости версии SDK), вы помещаете контейнер LinerLayout "Text2" в левой части контейнера Text1 ( android: layout_toLeftOf = "@+ id/text1" ). Если вы хотите добавить третий выравниватель контейнера, просто используйте этот последний атрибут относительно контейнера Text2 (android: layout_toLeftOf = "@+ id/text2" ) и т.д.

Надеюсь, это может вам помочь. Это выглядит так:

Пример расположения выравниваемых выравниваний по вертикали

Ответ 3

Просто установите ориентацию LinearLayout на горизонтальную

android:orientation="horizontal"