2017-10-15 227 views
1

已经寻找一个解决方案第一(例如here)我来到了以下解决方案:如何在RelativeLayout中水平居中按钮?

android:layout_centerHorizontal="true" 

不为我工作(见第三个按钮)。下面是RelativeLayout中三个按钮的完整示例代码,其中中间按钮应居中(按钮为水平和垂直),另两个按钮应该对称放置在屏幕上,水平居中。但是,他们不是,使用我多次发现的建议解决方案。

那么我错过了什么?

全码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/checkscreen" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.impyiablue.checkpoint.CheckScreen"> 

    <RelativeLayout 
     android:id="@+id/content" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginTop="20dp" 
     android:layout_weight="1" 
     android:orientation="vertical"> 


     <Button 
      android:id="@+id/check_cancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/check_next" 
      android:textSize="20sp" 
      android:padding="25dip" 
      android:layout_alignParentTop="true" 
      android:layout_alignLeft="@+id/check_now" 
      android:layout_alignStart="@+id/check_now" 
      android:layout_marginTop="50dp" /> 

     <Button 
      android:id="@+id/check_now" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:text="@string/check_now" 
      android:padding="70dip" 
      android:textSize="20sp" /> 

     <Button 
      android:id="@+id/check_redo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:text="@string/check_redo" 
      android:textSize="20sp" 
      android:padding="25dip" 
      android:layout_alignParentBottom="true" 
      android:layout_alignLeft="@+id/check_now" 
      android:layout_alignStart="@+id/check_now" 
      android:layout_marginBottom="50dp" /> 

    </RelativeLayout> 


</LinearLayout> 

Screenshot

+0

可能'android:layout_alignLeft'与'android:layout_centerHorizo​​ntal'发生冲突。如果你想中心水平为什么仍然使用布局对齐? – nhoxbypass

回答

2

`

<Button 
      android:id="@+id/check_redo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:text="@string/check_redo" 
      android:textSize="20sp" 
      android:padding="25dip" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="50dp" /> 

` 你可以尝试使用的RelativeLayout作为根布局,这会让事情变得更容易管理。

+0

好的建议,但这并不能解决我的实际问题... – Alex

+0

这是因为你定义了一些属性到其他按钮,即你使用相对定位,将它们移除它将工作, – himel

+0

0

我会建议你使用weightSum和layout_weight属性来水平创建3个按键,只显示中间的一个由另2施加无形的知名度(第一和最后一个)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/checkscreen" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" android:weightSum="9"> 
<Button android:layout_width="wrap_content" 
android:layout_height="match_parent" android:layout_weight="3" android:visibility="invisible"></Button> 
<Button android:layout_width="wrap_content" 
android:layout_height="match_parent" android:layout_weight="3" android:visibility="visible"></Button> 
<Button android:layout_width="wrap_content" 
android:layout_height="match_parent" android:layout_weight="3" android:visibility="invisible"></Button> 
</LinearLayout> 

请添加任何必要的参数这段代码只是为了证明这个想法。

0

在该button上使用android:layout_gravity=center。实施例 -

<Button 
      android:id="@+id/check_cancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/check_next" 
      android:textSize="20sp" 
      android:padding="25dip" 
      android:layout_gravity=center 
      android:layout_marginTop="50dp" /> 

或者居中一个parentview或布局的所有子视图的对准,只是上

0

布局这样应该使用的LinearLayout,因为它支持重量,同时RelativeLayout的没有按”进行父视图或布局使用android:gravity=center牛逼!在Android Studio中完全正常工作并测试版式文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/checkscreen" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="layout_centerHorizontal" 
    android:gravity="center" 
    android:orientation="vertical"> 


    <Button 
     android:id="@+id/check_cancel" 
     android:layout_width="wrap_content" 
     android:layout_height="0sp" 
     android:layout_marginTop="50dp" 
     android:layout_weight="1" 
     android:padding="25dip" 
     android:text="check_next" 
     android:textSize="20sp" /> 

    <Button 
     android:id="@+id/check_now" 
     android:layout_width="wrap_content" 
     android:layout_height="0sp" 
     android:layout_weight="1" 
     android:padding="70dip" 
     android:text="check_now" 
     android:textSize="20sp" /> 

    <Button 
     android:id="@+id/check_redo" 
     android:layout_width="wrap_content" 
     android:layout_height="0sp" 
     android:layout_marginBottom="50dp" 
     android:layout_weight="1" 
     android:padding="25dip" 
     android:text="check_redo" 
     android:textSize="20sp" /> 


</LinearLayout>