#android #css-float #android-linearlayout
#Android #css-float #android-linearlayout
Вопрос:
В CSS мы можем написать :
<div style="float:right"> Text1 </div>
<div style="float:right"> Text2 </div>
таким образом, справа появится Текст1 ..
Я пытаюсь сделать то же самое с 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>
Спасибо
Комментарии:
1. отображается ли оно сверху вниз?
2. @LAS_VEGAS ммм, макет в целом не работает, поэтому я просто пытаюсь имитировать float: right с помощью LinearLayout? Спасибо.
Ответ №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>
Комментарии:
1. Я думаю, вы просто физически поменяли Text2 на Text1? Я могу это сделать, но я не хочу? в CSS мы можем позволить элементу перемещаться вправо, даже если он написан первым!
2. Я также наложил гравитацию на родительский
3. вы дважды
layout_gravity="right"
повторили в родительском XML
Ответ №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″). Если вы хотите добавить 3-й контейнер, выровняйте его по правому краю, просто используйте этот последний атрибут относительно контейнера Text2 (android:layout_tolefto =»@ id / text2″) и так далее.
Надеюсь, это может вам помочь. Это выглядит как:
Комментарии:
1. Как я могу сделать разрыв строки? Я хочу, чтобы многие кнопки вели себя как свойство css-float 🙂
2. @StefanBrendle Вы можете использовать эту структуру внутри горизонтального линейного описания.
<LinearLayout ··· android:orientation="horizontal"> <RelativeLayout> ··· </RelativeLayout> <RelativeLayout> ··· </RelativeLayout> </LinearLayout>
Ответ №3:
Просто добавьте:
android:layout_gravity="right"
gravity="right"
для текста плавающий вправо, как выравнивание текста.
Ответ №4:
Просто установите ориентацию LinearLayout на горизонтальную
android:orientation="horizontal"