【问题标题】:Right Align button in horizontal LinearLayout水平线性布局中的右对齐按钮
【发布时间】:2012-05-31 08:20:05
【问题描述】:

如果您查看附加的图像。我需要我的按钮右对齐,但由于某种原因它不适用于“gravity:right”...

这是我用于该布局的代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

为什么不工作?!

【问题讨论】:

  • 在这种情况下尝试相对布局。 LL 不会工作,
  • 如果你想使用线性布局,使用两个内部线性布局,并使用 layout_weight 和 layout_gravity。它会正常工作的。
  • @VamsiChalla 有一个可能,请在下面查看我的新答案。

标签: android android-layout


【解决方案1】:

单个LinearLayout 解决方案。只添加一个简单的间距视图:
(似乎没有人提到这个,只有更复杂的多布局解决方案。)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <!-------------------------  ADDED SPACER VIEW -------------------->
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <!------------------------- /ADDED SPACER VIEW -------------------->

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

注意“突出显示”视图,我没有修改任何其他内容。 Height=0 确保它不可见。 width=0 是因为宽度是由LinearLayout 基于 weight=1 确定的。这意味着间隔视图将在LinearLayout 的方向(方向)上尽可能延伸。

请注意,如果您的 API 级别和/或依赖项允许,您应该使用 android.widget.Spaceandroid.support.v4.widget.Space 而不是 ViewSpace 以更便宜的方式完成了这项工作,因为它只测量而不尝试绘制像 View 那样的任何东西;它也更能表达意图。

【讨论】:

  • 只需将 android:layout_weight="1" 添加到 R.id.lblExpenseCancel 即可。
  • @h_rules 如果你想破解它,但想象一下当你向 TextView 添加背景时会发生什么。
  • 我想知道为什么这个答案被否决了,它有效而且很好,我一定会尝试的。
  • 这对移动开发者来说是完美的
  • @GeoMint layout_weight="1" 给它动态大小,这意味着“尽可能宽”(因为LinearLayouthorizontal)。 width=0dp is just for performance,它实际上不会是 0 宽。我有一种感觉,你甚至没有尝试过代码......
【解决方案2】:

只需添加 android:gravity="right" 这行父布局即可。

<LinearLayout
        android:id="@+id/withdrawbuttonContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        **android:gravity="right"**
        android:weightSum="5"
        android:visibility="visible">

        <Button
            android:id="@+id/bt_withDraw"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/BTN_ADD"
            app:delay="1500" />

        <Button
            android:id="@+id/btn_orderdetail_amend"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/BTN_CANCEL"
            app:delay="1500" />
</LinearLayout>

【讨论】:

    【解决方案3】:

    试试这个

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_gravity="right" 
        android:layout_height="wrap_content"             
        android:orientation="horizontal"  >
    
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
       android:layout_height="wrap_content"   
       android:orientation="horizontal" >
    
    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="9dp"
        android:text="cancel"
        android:textColor="#ffff0000"
        android:textSize="20sp" />
    
    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="15dp"
         android:textColor="#ff0000ff"
        android:text="add" />
    
     </RelativeLayout>
      </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      真正的解决方案:

      android:layout_weight="1" 用于 TextView,您的按钮向右移动!

      【讨论】:

        【解决方案5】:

        很晚才加入聚会,但标准方法是使用 ,它就是为此目的而创建的。

        所以在您的代码中,只需在 TextView 和 Button 之间插入以下内容:

        <Space
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="1" 
        />
        
        

        或者从调色板中,只需选择布局 > 空间。它会像上面的代码一样转储它,您只需要指定权重即可。

        【讨论】:

          【解决方案6】:

          使用下面的代码

          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="35dp"
              android:orientation="horizontal" >
          
              <TextView
                  android:id="@+id/lblExpenseCancel"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="10dp"
                  android:layout_marginTop="9dp"
                  android:text="@string/cancel"
                  android:textColor="#404040"
                  android:textSize="20sp" />
          
              <Button
                  android:id="@+id/btnAddExpense"
                  android:layout_width="wrap_content"
                  android:layout_height="45dp"
                  android:layout_alignParentRight="true"
                  android:layout_marginLeft="10dp"
                  android:layout_marginRight="15dp"
                  android:background="@drawable/stitch_button"
                  android:text="@string/add" />
          
          </RelativeLayout>
          

          【讨论】:

          • 在这种情况下,按钮似乎属性“layout_alignParentRight”不可用?可能已弃用?
          • 你使用相对布局而不是线性布局?
          • 啊废话.. 对不起,我完全看过去了。谢谢!
          • 仍然让我感到惊讶的是,尝试使用线性布局如何使看似如此简单的事情变得如此困难。
          • @DipakKeshariya 对于未来的观众,您可以使用 "android:layout_gravity="top|right" linearlayout
          【解决方案7】:

          我知道这个问题是不久前被问到的,但我以前做过这个,它允许在对齐项目时进行更多控制。如果你像 Web 编程一样看待它,它会有所帮助。您只需嵌套另一个LinearLayout,它将在其中包含您的按钮。然后,您可以更改按钮布局的重力并使其向右移动,而TextView 仍位于左侧。

          试试这个代码:

          <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"             
          android:orientation="horizontal" 
          android:layout_marginTop="35dp">
          
          <TextView
              android:id="@+id/lblExpenseCancel"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/cancel" 
              android:textColor="#404040"         
              android:layout_marginLeft="10dp" 
              android:textSize="20sp" 
              android:layout_marginTop="9dp"/>
          
          <LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:gravity="right"
              android:orientation="horizontal" >
          
              <Button
                  android:id="@+id/btnAddExpense"
                  android:layout_width="wrap_content"
                  android:layout_height="45dp"
                  android:background="@drawable/stitch_button"
                  android:layout_marginLeft="10dp"                    
                  android:text="@string/add" 
                  android:layout_gravity="right" 
                  android:layout_marginRight="15dp"/>
          
          </LinearLayout>
          

          您可能必须使用嵌套布局上的填充,以便它按照您的意愿行事。

          【讨论】:

          • 您应该为嵌套布局使用 FrameLayout。定位一个元素就是它的用途。而且,给它一个权重,让它填充父节点的正确部分。 +1 反正不使用相对布局。
          【解决方案8】:

          如果您不想或不能使用RelativeLayout,您可以将按钮包装在LinearLayout 中,方向为“vertical”,宽度为“fill_parent”。

          <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="35dp">
          
            <TextView
                android:id="@+id/lblExpenseCancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/cancel"
                android:textColor="#404040"
                android:layout_marginLeft="10dp"
                android:textSize="20sp"
                android:layout_marginTop="9dp" />
          
            <LinearLayout
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:orientation="vertical">
               <Button
                  android:id="@+id/btnAddExpense"
                  android:layout_width="wrap_content"
                  android:layout_height="45dp"
                  android:background="@drawable/stitch_button"
                  android:layout_marginLeft="10dp"
                  android:text="@string/add"
                  android:layout_gravity="right"
                  android:layout_marginRight="15dp" />
            </LinearLayout>
          </LinearLayout> 
          

          这是因为如果 LinearLayout 的方向是水平的,重力只会影响垂直视图。如果方向是“垂直的”,重力只会影响水平方向的视图。有关 LinearLayout 方向/重力解释的更多详细信息,请参阅here

          【讨论】:

            【解决方案9】:

            您需要在布局中添加重力而不是按钮,按钮设置中的重力是针对按钮内的文本

            <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_gravity="right" 
            android:layout_height="wrap_content"             
            android:orientation="horizontal" 
            android:layout_marginTop="35dp">
            

            【讨论】:

            • 那不是也对齐 TextView 吗?我只需要按钮右对齐...
            • 如果只需要对齐按钮,请使用RelativeLayout。
            • 即使您希望所有按钮对齐,这也行不通(API 16)。子视图仍然在左侧对齐。
            【解决方案10】:

            正如之前多次提到的:从 LinearLayout 切换到 RelativeLayout 是可行的,但您也可以解决问题而不是避免它。使用 LinearLayout 提供的工具:给 TextView 赋予 weight=1(参见下面的代码),按钮的权重应保持为 0 或无。在这种情况下,TextView 将占用所有剩余空间,这些空间不用于显示 TextView 或 ButtonView 的内容并将您的按钮推到右侧。

            <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginTop="35dp">
            
                    <TextView
                        android:id="@+id/lblExpenseCancel"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/cancel"
                        android:textColor="#404040"
                        android:layout_marginLeft="10dp"
                        android:textSize="20sp"
                        android:layout_marginTop="9dp"
            
                        **android:layout_weight="1"**
            
                        />
            
                    <Button
                        android:id="@+id/btnAddExpense"
                        android:layout_width="wrap_content"
                        android:layout_height="45dp"
                        android:background="@drawable/stitch_button"
                        android:layout_marginLeft="10dp"
                        android:text="@string/add"
                        android:layout_gravity="right"
                        android:layout_marginRight="15dp" />
            
                </LinearLayout>
            

            【讨论】:

            • 这是最好的答案。在找到你的帖子之前,我只是想写一些类似的东西。
            • 由于内存问题,这比切换到RelativeLayout 要好得多,因为RelativeLayout 消耗的内存比LinearLayout 多得多
            【解决方案11】:

            在按钮中使用布局宽度,如 android:layout_width="75dp"

            【讨论】:

              【解决方案12】:
              <LinearLayout
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"             
                  android:orientation="horizontal" 
                  android:layout_marginTop="35dp">
              
                  <TextView
                      android:id="@+id/lblExpenseCancel"
                      android:layout_width="0dp"
                      android:layout_weight="0.5"
                      android:layout_height="wrap_content"
                      android:text="@string/cancel" 
                      android:textColor="#404040"         
                      android:layout_marginLeft="10dp" 
                      android:textSize="20sp" 
                      android:layout_marginTop="9dp"/>              
              
                  <Button
                      android:id="@+id/btnAddExpense"
                      android:layout_width="0dp"
                      android:layout_weight="0.5"
                      android:layout_height="wrap_content"
                      android:background="@drawable/stitch_button"
                      android:layout_marginLeft="10dp"                    
                      android:text="@string/add" 
                      android:layout_gravity="right" 
                      android:layout_marginRight="15dp"/>
              
              </LinearLayout>
              

              这会解决你的问题

              【讨论】:

                【解决方案13】:

                我使用了类似的布局,有 4 个TextViews。两个TextViews 应该左对齐,两个TextViews 应该右对齐。所以,如果你想单独使用LinearLayouts,这是我的解决方案。

                <?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:baselineAligned="false"
                    android:padding="10dp" >
                
                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_weight="0.50"
                        android:layout_height="match_parent"
                        android:gravity="left"
                        android:orientation="horizontal" >
                
                        <TextView
                            android:id="@+id/textview_fragment_mtfstatus_level"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/level"
                            android:textAppearance="?android:attr/textAppearanceMedium" />
                
                        <TextView
                            android:id="@+id/textview_fragment_mtfstatus_level_count"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textAppearance="?android:attr/textAppearanceMedium" />
                    </LinearLayout>
                
                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_weight="0.50"
                        android:layout_height="match_parent"
                        android:gravity="right"
                        android:orientation="horizontal" >
                
                        <TextView
                            android:id="@+id/textview_fragment_mtfstatus_time"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/time"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                             />
                
                        <TextView
                            android:id="@+id/textview_fragment_mtfstatus_time_count"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=""
                            android:textAppearance="?android:attr/textAppearanceMedium" 
                            />
                    </LinearLayout>
                
                </LinearLayout>
                

                【讨论】:

                  【解决方案14】:

                  您可以在 Button 的父布局上使用RelativeLayout 或设置gravity="right"。

                  【讨论】:

                    【解决方案15】:

                    我知道这是旧的,但线性布局中的另一个是:

                    <LinearLayout
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginTop="35dp">
                    
                    <TextView
                        android:id="@+id/lblExpenseCancel"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/cancel"
                        android:textColor="#404040"
                        android:layout_marginLeft="10dp"
                        android:textSize="20sp"
                        android:layout_marginTop="9dp" />
                    
                    <Button
                        android:id="@+id/btnAddExpense"
                        android:layout_width="wrap_content"
                        android:layout_height="45dp"
                        android:background="@drawable/stitch_button"
                        android:layout_marginLeft="10dp"
                        android:text="@string/add"
                        android:layout_gravity="center_vertical|bottom|right|top"
                        android:layout_marginRight="15dp" />
                    

                    请注意 layout_gravity 而不是重力。

                    【讨论】:

                    • center_vertical|bottom|top 怎么了?对我来说,它们看起来像是专属的对齐重力。
                    【解决方案16】:

                    您应该使用 Relativelayout 而不是 Linearlayout 作为主要布局。如果您使用Relativelayout 作为main,那么可以轻松处理右侧的按钮,因为相对布局为我们提供了alignParent right、left、top 和bottom。

                    【讨论】:

                      【解决方案17】:
                          Adding Spacing as View , makes the Last textview to right in 
                          LinearLayout
                      
                          <LinearLayout
                              android:layout_width="match_parent"
                              android:layout_height="match_parent"
                              android:orientation="horizontal">
                           <!-- ADD SPACER VIEW -->
                              <View
                                  android:layout_width="0dp"
                                  android:layout_height="0dp"
                                  android:layout_weight="1"
                                  />
                          <!-- /ADD SPACER VIEW -->
                      </LinearLayout>
                      

                      【讨论】:

                        猜你喜欢
                        • 2016-09-26
                        • 2017-04-05
                        • 2012-07-19
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2011-10-22
                        • 1970-01-01
                        • 2015-08-11
                        相关资源
                        最近更新 更多