2013-04-26 65 views
2

如何重疊Button而不是另一個RelativeLayoutandroid - 將按鈕重疊在另一個

Example

謝謝。

這是我試過的。謝謝

<RelativeLayout 
     android:id="@+id/category" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_gravity="center_vertical|center_horizontal|center" 
     android:gravity="center" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/btn1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignTop="@id/btn2" 
      android:layout_centerHorizontal="true" 
      android:layout_centerInParent="true" 
      android:layout_centerVertical="true" 
      android:src="@drawable/btn1" /> 

     <ImageView 
      android:id="@+id/btn2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/btn2" /> 

    </RelativeLayout> 
+0

你可以嘗試添加到BTN2負左邊距 – Blackbelt 2013-04-26 11:24:03

+1

你可以把保證金到第二個,也適用to_rightof屬性基於第一個第二個。 – Noundla 2013-04-26 11:24:53

回答

6

你可以通過設置保證金爲負值。

<?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="wrap_content"> 
      <ImageView android:id="@+id/btn1" 
       android:layout_width="100dp" 
       android:layout_height="50dp"/> 
      <ImageView android:layout_width="100dp" 
       android:layout_height="50dp" 
       android:layout_toRightOf="@id/btn1" 
       android:layout_marginLeft="-20dp"/> 
     </RelativeLayout> 
+0

謝謝。像魅力一樣工作 – dracula 2013-04-26 11:42:31

2

這是你如何實現它。 使用layout_margin屬性爲右邊的按鈕並給它一個負值。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/category" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<Button 
    android:id="@+id/btn1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="MyLargeBtn" /> 

<Button 
    android:id="@+id/btn2" 
    android:layout_toRightOf="@id/btn1" 
    android:layout_marginLeft="-10dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="SmallBtn" />