2014-07-15 122 views
2

我已经创建了一个活动并使用RelativeLayout,它默认在Android Studio中提供。在relativeLayout中相互重叠的元素

我想在我的TextView之前放置一个按钮,但我无法这样做。

下面是结果:

enter image description here

这里是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".MyActivity"> 

    <TextView 
     android:id="@+id/heading" 
     android:text="@string/hello_world" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    <Button 
     android:id="@+id/button" 
     android:text="@string/sachin_jain" 
     android:layout_width="wrap_content" 
     android:layout_height="20dp" 
     android:layout_alignLeft="@+id/heading" 
     android:layout_alignTop="@+id/heading" 
     android:layout_centerHorizontal="true" 
     /> 

</RelativeLayout> 

看起来像我失去了一些东西。任何帮助,将不胜感激!!

+0

的可能的复制[如何把在同一个xml布局中彼此之上的元素?](http://stackoverflow.com/questions/78883 37/how-to-put-elements-on-each-other-in-the-same-xml-layout) – Trilarion

回答

6

我已经找到了解决的办法是:使用android:layout_below代替android:layout_alignTop/android:layout_alignRight

<TextView 
    android:id="@+id/heading" 
    android:text="My First Activity with Relative Layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    /> 

<Button 
    android:id="@+id/button" 
    android:text="Save" 
    android:layout_width="300px" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/heading" 
    android:layout_margin="20px" 
    android:layout_centerHorizontal="true" 
    /> 

下面是截图:

Snapshot with working Relative Layout

1

我想你首先需要一个按钮,第二个是textview。在您的按钮这种结构使得海誓山盟的视图顶部对齐

android:layout_alignLeft="@+id/heading" 
    android:layout_alignTop="@+id/heading" 

:然后你就可以做这样的事情:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MyActivity"> 

<TextView 
    android:id="@+id/heading" 
    android:text="@string/hello_world" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 

<Button 
    android:id="@+id/button" 
    android:text="@string/sachin_jain" 
    android:layout_width="wrap_content" 
    android:layout_height="20dp" 
    android:layout_alignRight="@+id/heading" 
    android:layout_alignTop="@+id/heading" 
    android:layout_centerHorizontal="true" 
    /> 

</RelativeLayout> 

你的问题是,你有。

+1

..您的解决方案无法正常工作。 'layout_alignRight'和'layout_alignTop'会将按钮的顶部和右侧与文本视图对齐,因此它们会再次相互重叠 – sachinjain024