【问题标题】:How to right align widget in horizontal linear layout Android?如何在水平线性布局Android中右对齐小部件?
【发布时间】:2011-10-22 23:55:08
【问题描述】:

这是我正在使用的代码,但它不起作用:

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

    <TextView android:text="TextView" android:id="@+id/textView1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:gravity="right">
    </TextView>

</LinearLayout>

【问题讨论】:

    标签: android alignment


    【解决方案1】:

    尝试在水平LinearLayout 中添加空的View 在您想要正确查看的元素之前,例如:

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

    【讨论】:

    • 这行得通!但任何人都可以解释为什么?我无法完全理解。
    • Empty View 试图占用最大的空间,为按钮留出空间。
    • 到目前为止,这是唯一对我有用的方法,即尝试在左侧设置几个按钮,在右侧设置最后一个按钮。但这对我来说就像一个黑客。有没有“正确”的方法来做到这一点?
    • 这太棒了!但是为什么gravity="right" 从一开始就不是这样工作的呢?为什么它需要这种另一种观点的帮助?如果没有额外的观点,它又如何“正确”?
    • 这在类似网格的布局中不起作用 - 没有空白空间。 Sherif elKhatib 的解决方案有效。
    【解决方案2】:

    如果您不希望所有内容都向右,请不要将 LinearLayout 的重力更改为“向右”。

    试试:

    1. 将TextView的width改为fill_parent
    2. 将TextView的gravity改为right

    代码:

        <TextView 
                  android:text="TextView" 
                  android:id="@+id/textView1"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"   
                  android:gravity="right">
        </TextView>
    

    【讨论】:

    • 谢谢 - 让我失望的是 fill_parent 的宽度
    • 线性布局是不是就是为了达到这个目的而设计的?不应该只使用相对布局来实现这一点吗?这个解决方案不是黑客吗?
    • 如果您在 LinearLayout 中有多个 TextView 控件,这将不起作用。查看@alcsan 的回答
    • 这应该是公认的答案。这种方法不使用任何额外的视图,更重要的是,它不使用显着降低性能的 layout_weight。
    • android:layout_weight = "1"android:gravity="right" 并排,应该可以解决问题。
    【解决方案3】:

    作为对alcsan答案的补充,您可以使用Space,因为API 14(Android 4.0 ICE_CREAM_SANDWICH),document here

    Space 是一个轻量级的 View 子类,可用于在通用布局中创建组件之间的间隙。

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

    对于支持 API 级别低于 14 的应用,自 Android 支持库 r22.1.0 起有一个android.support.v4.widget.Space

    【讨论】:

    • 重要的是你需要设置 layout_weight = "1"
    • 完美运行!保留每个元素的宽度。
    • 这行得通。哇,Android 的丑陋总是让我惊讶
    • 我不确定你是否需要重力属性。我没有。只要将 Space 元素的 layout_weight 设置为 1,Space 就会占用 TextView 获得所需空间后剩下的任何水平空间,这会强制 TextView 一直向右(因为 Space 在xml).
    • Android Studio 现在在 LinearLayout 中使用“Space”元素时会报告警告用“View”元素替换似乎可以解决问题。就我而言,我将布局更改为 ConstraintLayout,它也可以正常工作
    【解决方案4】:

    线性布局

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

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

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

    【讨论】:

      【解决方案5】:

      设置视图的layout_weight="1" 就可以了。!

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

      【讨论】:

      • 这么简单的解决方案+1
      • 如果有人还在寻找,这是正确的解决方案 :)
      • 这太神奇了!你是怎么发现的?
      • @andreikashin,如果有帮助,请考虑投赞成票。谢谢。
      • 这不是按钮等组件的解决方案
      【解决方案6】:

      android:gravity="right" 添加到LinearLayout。假设TextViewlayout_width="wrap_content"

      【讨论】:

      • 他特别说要对齐LinearLayout里面的单个组件。不是线性布局本身:布局本身设置为 fill_parent。
      【解决方案7】:

      只需在您的 Liner 布局中添加 android:gravity="right"

      【讨论】:

        【解决方案8】:

        对于水平方向的LinearLayout,将layout_weight 分配给除要右对齐的视图之外的其他子视图。这非常有效。

         <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >
            
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Specialization"
                        />
            
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Right"
                        android:textColor="#ff0000" />
            
            </LinearLayout>
        

        【讨论】:

          【解决方案9】:

          我用最简单的方法做到了:

          只需取一个 RelativeLayout 并将您的 child view 放入其中,您希望将其放置在 right一边。

              <LinearLayout
                  android:id="@+id/llMain"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="#f5f4f4"
                  android:gravity="center_vertical"
                  android:orientation="horizontal"
                  android:paddingBottom="20dp"
                  android:paddingLeft="15dp"
                  android:paddingRight="15dp"
                  android:paddingTop="20dp">
          
                  <ImageView
                      android:id="@+id/ivOne"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:src="@drawable/ic_launcher" />
          
          
                  <TextView
                      android:id="@+id/txtOne"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="20dp"
                      android:text="Hiren"
                      android:textAppearance="@android:style/TextAppearance.Medium"
                      android:textColor="@android:color/black" />
          
                  <RelativeLayout
                      android:id="@+id/rlRight"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:gravity="right">
          
          
                      <ImageView
                          android:id="@+id/ivRight"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:padding="5dp"
                          android:src="@drawable/ic_launcher" />
          
                  </RelativeLayout>
              </LinearLayout>
          

          希望对你有所帮助。

          【讨论】:

            【解决方案10】:

            linear layoutlayout_width="fill_parent" 以及具有相同 layout width + gravity as right 的小部件会将其对齐到右侧。

            我在以下示例中使用 2 个TextViews,左侧为topicTitle,右侧为topicQuestions

            <?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            
                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="20dp"
                    android:orientation="horizontal">
            
                <TextView
                    android:id="@+id/topicTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18sp"
                    android:textStyle="bold" />
            
                <TextView
                    android:id="@+id/topicQuestions"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:textSize="18sp"
                    android:textStyle="bold" />
                </LinearLayout>
            
            </RelativeLayout>
            

            输出

            【讨论】:

              【解决方案11】:

              无需使用任何额外的视图或元素:

              //就是这么简单

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

              //这是左对齐

              <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="No. of Travellers"
                    android:textColor="#000000"
                    android:layout_weight="1"
                    android:textStyle="bold"
                    android:textAlignment="textStart"
                    android:gravity="start" />
              

              //这是右对齐

              <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Done"
                    android:textStyle="bold"
                    android:textColor="@color/colorPrimary"
                    android:layout_weight="1"
                    android:textAlignment="textEnd"
                    android:gravity="end" />
              
              </LinearLayout>
              

              【讨论】:

                【解决方案12】:

                你应该使用RelativeLayout,然后拖动它们直到看起来不错:)

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

                【讨论】:

                • “拖动它们”不推荐用于多分辨率屏幕
                【解决方案13】:

                尝试将 layout_width 更改为 android:layout_width="match_parent",因为 gravity:"right" 对齐 layout_width 内的文本,如果您选择 wrap content 它没有去哪里,但如果您选择 match parent 它可以向右。

                【讨论】:

                  【解决方案14】:

                  如果是 TextView:

                  <TextView 
                      android:text="TextView" 
                      android:id="@+id/textView"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"   
                      android:gravity="right"
                      android:textAlignment="gravity">    
                  </TextView>
                  

                  【讨论】:

                    【解决方案15】:

                    添加视图有点困难,它覆盖了所有屏幕宽度,如下所示:

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

                    试试这个代码:

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

                    【讨论】:

                      【解决方案16】:

                      要在LinearLayout 的开头对齐一个元素,在末尾对齐一个元素,您可以将其包装在RelativeLayout 中。

                       <androidx.appcompat.widget.LinearLayoutCompat
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:orientation="horizontal"
                          android:layout_margin="8dp"
                          android:weightSum="2">
                      
                          <RelativeLayout
                              android:layout_width="0dp"
                              android:layout_height="wrap_content"
                              android:layout_weight="1"
                              android:gravity="start">
                              <com.google.android.material.button.MaterialButton
                                  android:layout_width="wrap_content"
                                  android:layout_height="wrap_content"
                                  android:text="Cancel"
                                  android:textColor="@android:color/background_dark"
                                  android:backgroundTint="@android:color/transparent"/>
                          </RelativeLayout>
                      
                          <RelativeLayout
                              android:layout_width="0dp"
                              android:layout_height="wrap_content"
                              android:layout_weight="1"
                              android:gravity="end">
                              <com.google.android.material.button.MaterialButton
                                  android:layout_width="wrap_content"
                                  android:layout_height="wrap_content"
                                  android:textColor="@android:color/background_dark"
                                  android:backgroundTint="@android:color/transparent"
                                  android:text="Save"/>
                          </RelativeLayout>
                      </androidx.appcompat.widget.LinearLayoutCompat>
                      

                      这个例子的结果如下: Link to the image

                      注意:你可以将任何你想要的东西包裹在里面并对齐。

                      【讨论】:

                        【解决方案17】:

                        这是我的 xml,要右对齐的动态组件,在我的例子中,我使用 3 个按钮

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

                        结果

                        您可以通过更改可见性 GONE 来隐藏右侧的第一个按钮,这是我的代码

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

                        仍然右对齐,可见性消失后第一个右组件

                        【讨论】:

                          【解决方案18】:

                          这是一个示例。安排的关键如下

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

                          完整代码

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

                          【讨论】:

                            【解决方案19】:

                            使用 match_parent 和重力将 TextView 文本设置为正确,如下所示:

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

                            【讨论】:

                              【解决方案20】:

                              试试这个..

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

                              【讨论】:

                                猜你喜欢
                                • 1970-01-01
                                • 2012-05-08
                                • 2015-12-01
                                • 2014-10-24
                                • 2011-12-02
                                • 2014-01-14
                                • 2016-09-26
                                • 2017-12-07
                                • 1970-01-01
                                相关资源
                                最近更新 更多