2013-05-12 67 views
12

根据另一个已存在的视图,是否可以使用RelativeLayout居中水平或垂直对齐XML中的视图。RelativeLayout:align相对于其他视图居中水平或垂直

例如:可以说有这样的事情:

enter image description here

第二个文本视图应显示为中心的第一个文本视图下方:

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

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="72dp" 
     android:text="dynamic text" /> 

    <TextView 
     android:id="@+id/second" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/textView" 
     android:layout_marginLeft="43dp" <!-- Is there a rule to center it? --> 
     android:text="centered below text 1" /> 

</RelativeLayout> 

是有可能实现类似于XML中的东西?有没有规则,我错过了?我不想计算位置编程

回答

12

以下内容使用一个单独的父布局的意见,并在主布局添加它(可以包含其他的事情,如果你有)

<?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="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center" 
     android:layout_marginLeft = "30dp"> 
     <TextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="dynamic text" /> 

     <TextView 
      android:id="@+id/second" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="centered below text 1" /> 
    </LinearLayout> 
... 
... 
other veiws go here 
... 

</RelativeLayout> 
+14

不,我不想将它居于父母 – sockeqwe 2013-05-12 19:00:42

+0

你需要它 – Noundla 2013-05-12 19:01:18

+0

我修改了答案。检查一次。 – Noundla 2013-05-12 19:04:59

4

使用适合你

android:layout_centerHorizontal="true" 
    android:layout_centerInParent="true"  
    android:layout_centerVertical="true"  
+4

不,我不想居中父...我要居中低于或高于一定的另一种观点。例如:另一个视图有一个marginLeft =“72dp” – sockeqwe 2013-05-12 18:47:56

+0

是的只是使用centerhoarizontal = true并将视图置于另一个之上或之下 – stinepike 2013-05-12 18:49:03

+0

嗯,也许我弄错了,但是应用这个将父母中的第二个文本视图居中。但我希望它被集中在其他视图下面,所以根据第一个视图居中相对是我想要的......我想这可以通过将这两个视图包装在另一个RelativeLayout – sockeqwe 2013-05-12 19:00:08

0

不要这个 - >

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

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="dynamic text" /> 

    <TextView 
     android:id="@+id/second" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView" 
     android:layout_centerHorizontal="true" <!-- Is there a rule to center it? --> 
     android:text="centered below text 1" /> 

</RelativeLayout> 
0

我找到了这样lution: 包裹内容到另一个RelativeLayout的,然后你可以把这个LayoutWrapper无论你想:

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

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="58dp" 
     android:layout_marginTop="58dp" > 

     <TextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerInParent="true" 
      android:text="dynamic text" /> 

     <TextView 
      android:id="@+id/second" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textView" 
      android:layout_centerInParent="true" 
      android:text="centered below text 1" /> 
    </RelativeLayout> 

</RelativeLayout> 
20

我比接受一个更好的解决方案。没有额外的睡觉!您可以通过在较小的视图上组合两个属性来完成此操作。如果您正在水平居中,则可以将align_start & align_end同时用于较大视图。确保文本重心居中顺便说一句。“机器人:重力=”中心”垂直对齐同时使用ALIGN_TOP & ALIGN_BOTTOM下面是布局的修改实施

<?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" 
android:paddingLeft="43dp" > 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignStart="@+id/second" 
    android:layout_alignEnd="@+id/second" 
    android:gravity="center" 
    android:text="dynamic text" /> 

<TextView 
    android:id="@+id/second" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textView" 
    android:gravity="center" 
    android:text="centered below text 1" /> 

</RelativeLayout> 

无需像接受不必要的嵌套。的回答。

+0

是的,但只有当你知道textview的宽度来调整边距时才有效。 – sockeqwe 2014-10-15 20:13:56

+0

没有必要保证金....保证金是你最初的。会发生什么情况是,较小的文本的宽度等于较大的文本,并且通过具有重心,视图将相对于较大的文本视图居中。要验证这一点,请添加第二个文本视图的边距或文本内容,并观察第一个移动。 – Jessicardo 2014-10-15 20:50:13

+0

我也删除了显式边距,添加了填充到布局容器,并更新了布局。 – Jessicardo 2014-10-15 20:55:49

0

正确的解决方案是使用ConstraintLayout

我试图对准一个TextView水平低于在RelativeLayout的一个按钮,但它是不可能的,因为ALIGN_BOTTOM和layout_below没有发挥得很好。最后我PI启动了constraintLayout并尝试了相同的逻辑,并且它像魅力一样工作。这里是下面的代码。

<android.support.constraint.ConstraintLayout 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:clipChildren="false" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<Button 
    android:id="@+id/episode_recording_header_stop" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@mipmap/ic_launcher" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintVertical_bias="0.399" /> 


<TextView 
    android:id="@+id/button_selected_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="8dp" 
    android:textAlignment="center" 
    android:textColor="@android:color/black" 
    android:textSize="12sp" 
    android:text="text which is exactly center aligned w.r.t the Button above" 
    app:layout_constraintEnd_toEndOf="@+id/episode_recording_header_stop" 
    app:layout_constraintStart_toStartOf="@+id/episode_recording_header_stop" 
    app:layout_constraintTop_toBottomOf="@+id/episode_recording_header_stop" 
    /> 

</android.support.constraint.ConstraintLayout> 

最终的输出连接下面

enter image description here

+0

正如你所看到的对齐是w.r.t锚点视图(按钮)而不是父视图或任何其他视图。 – 2018-01-04 06:58:52

+0

是的,只是使用ConstraintLayout,因为它允许做类似的东西(RelativeLayout不)... – sockeqwe 2018-01-04 07:19:23

+0

@sockeqwe接受的答案不回答问题的权利?另外,我花了一些时间才意识到在另一个视图中,视图的水平对齐在RelativeLayout中是不可能的。正确的答案是现在使用ConstraintLayout。 – 2018-01-04 07:40:17