2010-11-15 117 views
45

我尝试将两个按钮(带有可正常工作的图像)并排放置并水平对中。这是我迄今为止:水平居中两个按钮

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

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

编辑

遗憾的是没有任何意见或建议的工作至今。这就是为什么我现在尝试提供一种简化的,完整的布局与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和没有运气的按钮元素属性的所有组合。任何其他想法?

+0

有多大的按钮?他们会像你想要他们一样肩并肩吗? – prolink007 2010-11-15 23:32:46

+0

是的,这完全没有问题。目前他们并排出现,但他们坚持左侧。 – Yves 2010-11-15 23:36:29

+0

哦,我明白你的问题是什么,我错过了这个问题。我以为你说你不能让他们水平排队。 – prolink007 2010-11-15 23:38:24

回答

94

这是我的解决方案:

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

辉煌答案!看起来就像那些市场上的应用程序。非常感谢! – evandrix 2011-07-14 23:26:03

+7

这里的秘密酱汁是'android:layout_weight =“1.0”'为水平LinearLayout中的每个按钮指定此值,并且所有按钮的大小相同。 – 2011-12-16 08:39:14

+0

那么这是完美的,我们可以做到这一点,而不使用内部线性布局。由于我有类似的问题,但有6到7行,每个文本视图有2个。活动渲染速度非常缓慢,有人告诉我是否可以缩小布局树,而不是渲染速度更快。 – 2012-02-15 11:11:05

4

属性名称android:layout_foo是LayoutParams - 视图父项的参数。尝试在LinearLayout上设置android:gravity="center"而不是android:layout_gravity。其中一个影响LinearLayout将如何布置自己的孩子,另一个会影响父母应该如何对待它。你想要前者。

+0

没有工作。仍然对齐左侧。 – Yves 2010-11-16 08:05:36

+0

为我工作...在相对布局中对齐两个元素...您希望两个元素以中心线为中心的位置....用重力=中心放置在水平线上,然后将控件放入内部那线性布局 – lepert 2013-09-20 01:18:08

1

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

15

我不知道,如果你确实找到了一个很好的回答这个问题不断,但我通过制作的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> 
+0

那么如何将它们卡在屏幕的底部? TableRow不会为此工作 – 2016-09-24 20:12:02

30

我知道你已经找到了解决方案,但你也可能会觉得这很有用。通过增加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> 

截图结果:

screen shot of the result

+1

这种技术效果很好。谢谢 – speedynomads 2013-05-14 11:23:50

+2

我发现它比创建额外的线性布局更好,谢谢! – zhaocong 2013-11-20 02:44:49

+0

这个不适合我,出于某种原因。我只是在'ScrollView'内使用'RelativeLayout'。 – 2017-04-11 19:04:50

2

这是我做的,我裹着这有它的重心设置为CENTER_HORIZONTAL线性布局的线性布局。然后,我将包装的线性布局宽度设置为组合的按钮的像素宽度。在这个例子中,按钮宽度为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> 
7

如果重新寻找一个快速全面的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> 
1

我觉得你的痛苦,我不得不调整一下,让这个工作。

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

这个工作很适合我:

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

我裹着两条水平的LinearLayout的如下:

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

这看起来像一个非常古老的问题ñ,但我也有同样的问题。我必须在屏幕中间放置两个彼此相邻的按钮(水平)。我最终将两个按钮放置在线性布局中,并将线性布局放置在相对布局中,其重力设置为中心。


更新:我发现了一个更简单的解决方案:

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

我中心两个按钮这样:

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

使用Relatie Layout代替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> 

enter image description here

+0

我可否请您知道投票的理由? – 2014-06-12 10:34:42

1

最优雅的方式去这不会引入冗余组件或视图组是使用间距。您可以使用线性布局内的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分配给它们,所以它们不会占用任何额外的空间,只是它们明确给出的(图像的大小)。

+0

这仅适用于API> = 14。 – 2016-07-15 11:00:32

0

下面的代码将对齐中心水平的两个按钮,没有给出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> 
2

这是使用的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> 
+0

简单易行的解决方案。谢谢。 – 2017-05-22 07:31:25

0

这个工作对我来说。简单和容易。

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

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

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

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