How to right align widget in horizontal linear layout Android?

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

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




        
相关标签:
19条回答
  • Adding view is bit difficult and it cover all screen width like this:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />                
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    Try to this code:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        >
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Create Account"/>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-12 10:58

    As a supplement to alcsan's answer, you can use Space since API 14 (Android 4.0 ICE_CREAM_SANDWICH), document here.

    Space is a lightweight View subclass that may be used to create gaps between components in general purpose layouts.

    <?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" >
    
        <Space
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <TextView
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="TextView"
            android:gravity="right" />
    
    </LinearLayout>
    

    For apps that supporting API levels under 14, there is an android.support.v4.widget.Space since Android Support Library r22.1.0.

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

    Here is a sample. the key to arrange is as follows

    android:layout_width="0dp"
    android:layout_weight="1"
    

    Complete code

    <?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="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp">
    
        <TextView
            android:id="@+id/categoryName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="abcd" />
    
        <TextView
            android:id="@+id/spareName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="efgh" />
    
    </LinearLayout>
    

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

    You should use a RelativeLayout and just drag them until it looks good :)

        <ImageView
            android:id="@+id/button_info"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:contentDescription="@string/pizza"
            android:src="@drawable/header_info_button" />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-12 11:00

    Try This..

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:orientation="horizontal" 
        android:gravity="right" >
    
    
    
        <TextView android:text="TextView" android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-12 11:01

    Try to change the layout_width to android:layout_width="match_parent" because gravity:"right" aligns the text inside the layout_width, and if you choose wrap content it does not have where to go, but if you choose match parent it can go to the right.

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