【问题标题】:Center two buttons horizontally水平居中两个按钮
【发布时间】:2010-11-15 23:30:15
【问题描述】:

我尝试将两个按钮(上面有可以正常工作的图像)排列在一起,并将它们水平居中。这就是我目前所拥有的:

<LinearLayout android:orientation="horizontal" android:layout_below="@id/radioGroup"
              android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal|center">
    <Button
            android:id="@+id/allow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/radioGroup"
            android:layout_gravity="center_horizontal"
            android:drawableLeft="@drawable/accept_btn"
            android:text="Allow"/>
    <Button
            android:id="@+id/deny"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/allow"
            android:layout_below="@id/radioGroup"
            android:layout_gravity="center_horizontal"
            android:drawableLeft="@drawable/block_btn"
            android:text="Deny"/>
</LinearLayout>

不幸的是,它们仍然与左侧对齐。任何帮助表示赞赏!伊夫

编辑

不幸的是,到目前为止,没有任何 cmets 或建议起作用。这就是为什么我现在尝试使用 RelativeLayout 来提供简化的完整布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" android:layout_height="wrap_content"
            android:layout_centerHorizontal="true">
    <TextView android:text="@+id/TextView01" android:id="@+id/TextView01"
          android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/allow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/TextView01"
        android:text="Allow"/>
    <Button
        android:id="@+id/deny"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/allow"
        android:layout_alignTop="@id/allow"
        android:text="Deny"/>
</RelativeLayout>

我已经尝试了 LinearLayout 和 Button 元素中的所有属性组合,但没有运气。还有其他想法吗?

【问题讨论】:

  • 按钮有多大?它们会像您想要的那样并排放置吗?
  • 是的,这完全没有问题。目前它们并排出现,但它们坚持在左侧。
  • 哦,我明白你的问题是什么了,我看错了问题。我以为你是说你不能让它们水平排列。

标签: android layout


【解决方案1】:

这是我的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_centerHorizontal="true">

    <TextView
        android:text="@+id/SomeText"
        android:id="@+id/TextView01"
        android:layout_width="wrap_content" android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:background="@android:drawable/bottom_bar"
        android:paddingLeft="4.0dip"
        android:paddingTop="5.0dip"
        android:paddingRight="4.0dip"
        android:paddingBottom="1.0dip"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_below="@+id/TextView01">

        <Button
            android:id="@+id/allow"
            android:layout_width="0.0dip" android:layout_height="fill_parent"
            android:text="Allow"
            android:layout_weight="1.0" />

        <Button
            android:id="@+id/deny"
            android:layout_width="0.0dip" android:layout_height="fill_parent"
            android:text="Deny"
            android:layout_weight="1.0" />

    </LinearLayout>
</RelativeLayout>

【讨论】:

  • 出色的答案!看起来就像市场上的那些应用程序。谢谢一百万!
  • 这里的秘诀是android:layout_weight="1.0" 为水平线性布局中的每个按钮指定这个,所有按钮的大小都相同。
  • 很好,我们可以不使用内部线性布局来做到这一点。因为我有类似的问题,但是 6 到 7 行,每行有 2 个文本视图。 Activity 渲染非常慢,有人告诉我如果我可以减少布局树,它可能会渲染得更快。
  • 这会在不需要时创建一个LinearLayout。我发现@RHughes 的解决方案更优雅。
  • 这里的另一个秘诀是将按钮的宽度设置为“0.0dip”。
【解决方案2】:

我知道您已经找到了解决方案,但您可能也会发现这很有用。通过增加 dip 设置,用作中心参考的空 TextView 可以兼作按钮之间的填充。它还可以很好地处理屏幕方向的变化。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:text="Left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/centerPoint" />

    <TextView
        android:id="@+id/centerPoint"
        android:text=""
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/button2"
        android:text="Right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/centerPoint" />

</RelativeLayout>

结果截图:

【讨论】:

  • 由于某种原因,这个对我不起作用。我只是在ScrollView 中使用RelativeLayout
【解决方案3】:

我不知道你是否真的找到了这个问题的好答案,但我通过制作一个 TableRow 来实现它,将宽度设置为 fill_parent 并将重力设置为中心,然后将两个按钮放在其中。

    <TableRow android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:gravity="center">
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test" android:width="100dip"></Button>
    <Button android:layout_width="wrap_content" 
        android:text="Test" android:layout_height="wrap_content"
        android:width="100dip"></Button>
</TableRow>

【讨论】:

  • 那么如何将它们粘贴在屏幕底部? TableRow 不会为此工作
  • @YoushaAleayoub 为此在 tablerow 上使用 android:layout_alignParentBottom="true"
【解决方案4】:

如果您正在寻找一个快速的完全 RelativeLayout 解决方案,那么以下方法效果很好。 虚拟视图元素是不可见的并且水平居中。 两个按钮左对齐或右对齐。

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.be.android.stopwatch"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal">

<View android:id="@+id/dummy"  android:visibility="visible" android:layout_height="0dip" android:layout_width="1dip" android:background="#FFFFFF" android:layout_centerHorizontal="true"/>

<ImageButton android:id="@+id/button1" android:layout_height="25dip" android:layout_alignTop="@+id/dummy" android:layout_toLeftOf="@+id/dummy" android:layout_width="50dip"/>

<ImageButton android:id="@+id/button2" android:layout_height="25dip" android:layout_alignTop="@+id/dummy"  android:layout_toRightOf="@+id/dummy"   android:layout_width="50dip"/>

</RelativeLayout>

【讨论】:

    【解决方案5】:

    名为 android:layout_foo 的属性是 LayoutParams - 视图父级的参数。尝试在LinearLayout 上设置android:gravity="center" 而不是android:layout_gravity。一个影响LinearLayout 如何在其内部布置其子级,另一个影响LinearLayout 的父级应如何对待它。你想要前者。

    【讨论】:

    • 为我工作...在相对布局中对齐两个元素...您希望两个元素围绕中心线居中......放置在水平线性与重力 =居中,然后将控件放在该线性布局中
    【解决方案6】:

    我以这种方式居中两个按钮:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/one" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/two" />
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案7】:

      我已经包装了两个水平线性布局如下:

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

      您的 UI 小部件在这里

      </LinearLayout> </LinearLayout>
      

      【讨论】:

        【解决方案8】:

        使用 Relatie 布局而不是 Linear 并将重力设置为 android:gravity="center"

        如果您在不同尺寸的设备上运行应用程序,则相对布局可以提供更好的性能和更好的扩展性。

        以 XML 和生成的 UI 为例:

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:background="@Color/custom1"
                android:gravity="center" >
        
                <TextView
                    android:id="@+id/tv1"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:background="@color/Red"
                    android:gravity="center"
                    android:layout_marginRight="80dp"
                    android:text="Text11111" />
        
                <TextView
                    android:id="@+id/tv2"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_toRightOf="@id/tv1"
                    android:background="@color/Yellow"
                    android:gravity="center"
                    android:text="Text22222" />
            </RelativeLayout>
        

        【讨论】:

          【解决方案9】:

          这是我使用 relativeLayout 的解决方案:

          <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:gravity="center">
          
                  <Button
                      android:id="@+id/reject_btn"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/reject"/>
          
                  <Button
                      android:id="@+id/accept_btn"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="@string/accept"
                      android:layout_toRightOf="@id/reject_btn"/>
              </RelativeLayout>
          

          【讨论】:

            【解决方案10】:

            这似乎是一个非常古老的问题,但我也遇到了同样的问题。我不得不在屏幕中间(水平)放置两个按钮。我最终将两个按钮放置在线性布局中,并将线性布局放置在相对布局中,并将其重力设置为中心。


            [更新] 我找到了一个更简单的解决方案:

            <LinearLayout android:layout_height="wrap_content"
                          android:layout_width="wrap_content"
                          android:layout_centerHorizontal="true">
            
                <Button android:text="status"
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"/>
            
                <Button android:text="fly"
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"/>
            
            </LinearLayout>
            

            这是我之前的:

            <RelativeLayout android:layout_height="wrap_content"
                            android:layout_width="match_parent"
                            android:id="@+id/actionButtons"
                            android:layout_below="@id/tripInfo"
                            android:gravity="center">
            
                <LinearLayout android:layout_height="wrap_content"
                              android:layout_width="wrap_content">
            
                    <Button android:text="status"
                            android:layout_height="wrap_content" 
                            android:layout_width="wrap_content" />
            
                    <Button android:text="fly"
                            android:layout_height="wrap_content" 
                            android:layout_width="wrap_content" />
                </LinearLayout>
            
            </RelativeLayout>
            

            【讨论】:

              【解决方案11】:

              这就是我所做的,我用一个线性布局包裹了一个线性布局,它的重力设置为 center_horizo​​ntal。然后我将包装的线性布局宽度设置为组合按钮的像素宽度。在这个例子中,按钮是 100px 宽,所以我将包装的线性布局设置为 200px。

              示例 xml:

              <?xml version="1.0" encoding="utf-8"?>
              <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:gravity="center_horizontal">
                  <TextView android:layout_width="fill_parent"
                      android:layout_height="200px"
                      android:text="This is some text!"
                      android:background="#ff333333" />
                  <LinearLayout android:layout_height="wrap_content"
                      android:id="@+id/linearLayout1"
                      android:layout_width="200px">
                      <Button android:id="@+id/button1"
                      android:layout_height="wrap_content"
                      android:text="Button 1"
                      android:layout_width="100px">
                      </Button>
                      <Button android:id="@+id/button2"
                      android:layout_height="wrap_content"
                      android:text="Button 2"
                      android:layout_width="100px">
                      </Button>
                  </LinearLayout>
              </LinearLayout>
              

              【讨论】:

                【解决方案12】:

                在不引入冗余组件或视图组的情况下,最优雅的方法是使用间距。您可以在线性布局中使用带有 layout_weight 的空间元素来获得您想要的效果:

                <LinearLayout android:orientation="horizontal" android:layout_below="@id/radioGroup"
                        android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal|center">
                        <Space
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_weight="1" />
                        <Button
                            android:id="@+id/allow"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@id/radioGroup"
                            android:drawableLeft="@drawable/accept_btn"
                            android:text="Allow"/>
                        <Button
                            android:id="@+id/deny"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_toRightOf="@id/allow"
                            android:layout_below="@id/radioGroup"
                            android:drawableLeft="@drawable/block_btn"
                            android:text="Deny"/>
                        <Space
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_weight="1" />
                    </LinearLayout>
                

                给两个空间元素一个相等的 layout_weight(不管权重是什么,它只是将它们作为所有权重总和的比率)导致空间元素占用额外的可用空间。所以它将按钮强制到中间,没有为它们分配任何 layout_weight,因此它们不会占用任何额外的空间,只是它们明确给出的(图像的大小)。

                【讨论】:

                • 这仅适用于 API >= 14。
                【解决方案13】:

                我感觉到你的痛苦,我不得不稍微调整一下,让这个工作正常。

                <?xml version="1.0" encoding="utf-8"?>
                <RelativeLayout 
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                
                 <Button 
                     android:id="@+id/Button01"
                     android:layout_alignParentLeft="true"
                     android:layout_width="75dp"
                     android:layout_height="40dp"
                     android:layout_marginTop="15dp"
                     android:layout_marginLeft="60dp"
                     android:text="No" />
                
                 <Button 
                     android:id="@+id/Button02"
                     android:layout_alignParentRight="true"
                     android:layout_width="75dp"
                     android:layout_height="40dp"
                     android:layout_marginTop="15dp"
                     android:layout_marginRight="60dp"
                     android:text="Yes" />
                
                 <TextView
                    android:id="@+id/centerPoint"
                    android:layout_toRightOf="@id/Button01"
                    android:layout_toLeftOf="@id/Button02"
                    android:text=""
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    />
                
                </RelativeLayout>
                

                【讨论】:

                  【解决方案14】:

                  这对我来说效果很好:

                  <RadioGroup
                      android:id="@+id/ratingRadioGroup"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:orientation="horizontal"
                      android:layout_gravity="center_horizontal">
                      <RadioButton
                          android:id="@+id/likeButton"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:checked="true"
                          android:text="@string/like"
                          android:paddingRight="20pt"/>
                      <RadioButton
                          android:id="@+id/dislikeButton"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:text="@string/dislike" />
                  </RadioGroup>
                  

                  【讨论】:

                    【解决方案15】:

                    使用RelativeLayout 代替LinearLayout 并设置它的android_layout:gravity="center_horizontal" 或者一个非常非最佳的方法是设置LinearLayoutandroid_padding 与像素值对齐在中心。

                    【讨论】:

                      【解决方案16】:

                      下面的代码将水平居中对齐两个按钮,没有给出 dp。所以这将适用于所有方向和所有屏幕。

                      <LinearLayout
                          android:orientation="horizontal"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="center_horizontal"
                          >
                      
                          <Button
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:text="Change password"
                              android:id="@+id/change_pwd_button"
                              android:layout_gravity="center_horizontal"
                              android:background="@android:color/holo_blue_dark"
                              android:textColor="@android:color/white"
                      
                      
                              android:padding="25px"/>
                          <Button
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:text="Close"
                              android:id="@+id/change_pwd_close_btn"
                              android:layout_gravity="center_horizontal"
                              android:background="@android:color/holo_blue_dark"
                              android:textColor="@android:color/white"
                              android:layout_marginLeft="20dp"
                              />
                      </LinearLayout>
                      

                      【讨论】:

                        【解决方案17】:

                        这对我有用。简单易行。

                        <RelativeLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent">
                                        <LinearLayout
                                            android:layout_width="wrap_content"
                                            android:layout_height="wrap_content"
                                            android:layout_centerInParent="true"
                                            >
                                                <RelativeLayout
                                                    android:layout_width="wrap_content"
                                                    android:layout_height="wrap_content">
                                                        <Button
                                                            android:id="@+id/btn_one"
                                                            android:layout_width="wrap_content"
                                                            android:layout_height="wrap_content"
                                                            android:text="Button One"
                                                            />
                                                        <Button
                                                            android:id="@+id/btn_two"
                                                            android:layout_width="wrap_content"
                                                            android:layout_height="wrap_content"
                                                            android:text="Button Two"
                                                            android:layout_toRightOf="@id/btn_one"
                                                            />
                                                </RelativeLayout>
                                        </LinearLayout>
                                </RelativeLayout>
                        

                        【讨论】:

                          【解决方案18】:

                          尝试在中间放置一个高度和宽度为零的视图,并将其定位在中心,并将两个按钮定位在其右侧和左侧。

                          看看,这是我构建的应用程序的一部分:

                          <RelativeLayout
                                  android:layout_width="match_parent"
                                  android:layout_height="wrap_content">
                          
                          <View
                              android:layout_width="0dp"
                              android:layout_height="0dp"
                              android:layout_centerInParent="true"
                              android:id="@+id/view_in_middle">
                          </View>
                          
                          <Button
                          
                              android:layout_width="120dp"
                              android:layout_height="wrap_content"
                              android:id="@+id/yes_button"
                              android:layout_marginRight="24dp"
                              android:layout_toLeftOf="@id/view_in_middle"
                              android:text="Yes" />
                          
                          <Button
                          
                              android:layout_width="120dp"
                              android:layout_height="wrap_content"
                              android:id="@+id/no_button"
                              android:text="No"
                              android:layout_marginLeft="24dp"
                              android:layout_toRightOf="@id/view_in_middle"
                            />
                          
                          </RelativeLayout>
                          

                          【讨论】:

                            【解决方案19】:

                            试试这个,复制粘贴修改代码。

                            <LinearLayout
                                android:orientation="horizontal"
                                android:layout_gravity="center_horizontal"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content">
                            
                            <Button
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="40dp"   />
                            
                            <Button
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" />
                            
                            </LinearLayout>
                            

                            【讨论】:

                              猜你喜欢
                              • 2020-03-14
                              • 1970-01-01
                              • 2017-07-29
                              • 2013-03-02
                              • 2014-05-22
                              • 2016-08-15
                              • 1970-01-01
                              • 2013-11-15
                              • 1970-01-01
                              相关资源
                              最近更新 更多