2015-09-05 67 views
1

我需要3個部分的佈局的中心按鈕如下所示如何對齊屏幕重疊兩個佈局

enter image description here

它包含3個部分報頭,主體和頁腳和一個按鈕。我需要這個按鈕正好在屏幕中心,重疊頁腳和身體。 現在我正在設置固定的dp頁邊距來達到這個目的。但它在不同的屏幕上沒有正確對齊。我怎樣才能設置這個設計?我需要這個按鈕恰好在屏幕的中心。下面是我的佈局XML代碼

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:gravity="center_horizontal"> 

<RelativeLayout 
    android:background="@color/transparent_black" 
    android:id="@+id/header" 
    android:layout_width="match_parent" 
    android:layout_height="100dp" 
    /> 

<RelativeLayout 

    android:id="@+id/body" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@id/header" 
    android:layout_above="@+id/relativeLayout2" /> 

<RelativeLayout 
    android:background="@color/transparent_black" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/relativeLayout2"></RelativeLayout> 

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:src="@drawable/ic_plus" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignBottom="@id/body" 
    android:layout_marginBottom="-32dp" 
    android:layout_marginRight="160dp" /> 
</RelativeLayout> 
+0

這個問題年長會幫助你。 http://stackoverflow.com/questions/4182486/placing-overlappingz-index-a-view-above-another-view-in-android?answertab=active#tab-top – FiN

回答

2

試試這個

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:src="@drawable/ic_plus" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_alignBottom="@id/body" 
    android:layout_marginBottom="-32dp" /> 
+0

你可以看看PercentageRelativeLayout,但我自己並沒有使用它。 –