2014-07-15 126 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