How to right align widget in horizontal linear layout Android?

前端 未结 19 3070
温柔的废话
温柔的废话 2020-12-12 10:34

This is the code I am using and it is not working:




        
相关标签:
19条回答
  • 2020-12-12 10:45

    Use match_parent and gravity to set the TextView text to right, like this:

    <?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>
    
    0 讨论(0)
  • 2020-12-12 10:46

    With Linear Layout

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

    enter image description here

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

    with 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>
    
    0 讨论(0)
  • 2020-12-12 10:52

    Add android:gravity="right" to LinearLayout. Assuming the TextView has layout_width="wrap_content"

    0 讨论(0)
  • 2020-12-12 10:53

    setting the view's layout_weight="1" would do the trick.!

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

    0 讨论(0)
  • 2020-12-12 10:55

    just add android:gravity="right" in your Liner Layout.

    0 讨论(0)
  • 2020-12-12 10:55

    this is my xml, dynamic component to align right, in my case i use 3 button

     <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/checkinInputCodeMember">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="7"
                    android:orientation="vertical" />
    
                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/bttn_extends"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:textColor="@color/colorAccent"
                    android:text="3"/>
    
                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/bttn_checkout"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:textColor="@color/colorAccent"
                    android:text="2"/>
    
                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/checkinButtonScanQrCodeMember"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:textColor="@color/colorAccent"
                    android:text="1"/>
    
    
            </LinearLayout>
    

    and the result

    you can hide the right first button with change visibility GONE, and this my code

     <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/checkinInputCodeMember">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="7"
                    android:orientation="vertical" />
    
                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/bttn_extends"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:textColor="@color/colorAccent"
                    android:text="3"/>
    
                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/bttn_checkout"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:textColor="@color/colorAccent"
                    android:text="2"/>
    
                <androidx.appcompat.widget.AppCompatButton
                    android:id="@+id/checkinButtonScanQrCodeMember"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:text="1"
                    android:textColor="@color/colorAccent"
                    **android:visibility="gone"**/>
    
    
            </LinearLayout>
    

    still align right, after visibility gone first right component

    0 讨论(0)
提交回复
热议问题