简体   繁体   中英

How to align linear layout to right - chat message effect

I want to have my linear layout aligned to the right of the screen, however i cannot do this properly.

I want to have same effect as showed here: https://monosnap.com/file/ZXsqPJ71d61IHfK3JOyKjcMMZk2uWc#

I don't want to change anything else, only make it to be aligned to the right. Also i want to have the wrap content of the text of the message effect(not the sender name and date send), so it can be extendable to the left, if the text is long.

Here is the layout code for the item i wanna be on the right:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/single_message_view">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="author"
            android:id="@+id/sender_data"
            android:layout_marginEnd="5dp"
            android:textColor="@color/colorWhite" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:textAlignment="textEnd"
            android:layout_height="wrap_content"
            android:text="date"
            android:textColor="@color/colorWhite"
            android:id="@+id/send_date" />

    </LinearLayout>

<LinearLayout
    android:layout_marginEnd="15dp"
    android:layout_marginStart="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="100dp"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/message_content"
        android:background="@drawable/msg_haze"
        >

        <TextView
            android:layout_margin="10dp"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/colorWhite"
            android:id="@+id/message_text"
            android:text="content" />

        <LinearLayout
            android:layout_margin="10dp"
            android:minWidth="100dp"
            android:layout_below="@id/message_text"
            android:id="@+id/tags_container"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.RecyclerView
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:id="@+id/message_tags"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layoutManager="LinearLayoutManager"
                tools:listitem="@layout/item_tag" />
        </LinearLayout>

        <ImageView
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="10dp"
            android:paddingBottom="10dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:id="@+id/message_state" />

        <LinearLayout
            android:id="@+id/attachment_holder"
            android:layout_marginTop="10dp"
            android:padding="5dp"
            android:visibility="gone"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/attachments_haze">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/attachments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layoutManager="GridLayoutManager"/>

            <ImageView
                android:id="@+id/attachment"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </RelativeLayout>
</LinearLayout>

Your question is bit confusing but this may help you.

Their are two attributes you can use to align the elements

  • android:gravity sets the gravity of the content of the View its used on.

  • android:layout_gravity sets the gravity of the View or Layout in its parent.

And an example is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM