开发者

CSS "float:right" property equivalent in LinearLayout on android?

开发者 https://www.devze.com 2023-04-13 09:17 出处:网络
On CSS we can write : <div style=\"float:right\"> Text1 </div> <div style=\"float:right\"> Text2 </div>

On CSS we can write :

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

by this way Text1 will appear on the right ..

I'm trying to do the same with LinearLayout , the View should appear from right to left :

<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-->
      开发者_StackOverflow社区  <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>

Thanks


This may be it

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


Don't know if it is possible with LinearLayout, but you can achieve what you need with a RelativeLayout like this:

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

In relative layout you can align the "text1" layout container to right side relative to parent (android:layout_alignParentEnd="true" or android:layout_alignParentRight="true" depending on SDK version compatibility), then you place the "Text2" container LinerLayout at the left side of the Text1 container (android:layout_toLeftOf="@+id/text1"). If you want to add a 3rd container align right just use this last attribute relative to Text2 container (android:layout_toLeftOf="@+id/text2") and so on.

Hope this can help you. It looks like:

CSS "float:right" property equivalent in LinearLayout on android?


Just add:

android:layout_gravity="right"

gravity="right" is for the text to float right, like text alignment.


Just set the LinearLayout orientation to Horizontal

android:orientation="horizontal"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号