2013-04-10 37 views
1

我想在FrameLayout中將兩個按鈕添加到另一個之下,但它們應該是觸摸的。但是,我沒有設法做到這一點。這裏是我的代碼:添加兩個在FrameLayout中相互接觸的按鈕

<FrameLayout 
    android:id="@+id/lytOptions" 
    android:layout_below="@id/imgLine" 
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content" 
    > 

    <Button 
     android:id="@+id/btnLogin" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/action_via_email" 
     android:textColor="@android:color/white" 
     android:textStyle="bold" /> 

    <Button 
     android:id="@+id/btnSignUp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"    
     android:text="@string/action_sign_up" 
     android:textColor="@android:color/white" 
     android:textStyle="bold" /> 
</FrameLayout> 
+0

你能澄清你所說的「既要感動」請什麼意思?這些按鈕應該彼此相鄰嗎?還是彼此重疊? – 2013-04-10 07:31:49

+0

尼爾按鈕邊框應該被觸及。我不希望兩個按鈕之間有任何空間。 – 2013-04-10 07:33:18

+0

我已經編輯了這個問題來說明問題,假設我的編輯已被接受。 – 2013-04-10 07:43:02

回答

1

試試這個

 <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

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

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="49dp" 
      android:text="Button" /> 

    </RelativeLayout> 

得到輸出這樣

enter image description here

+0

正確解決了這個問題。謝謝Sanghita :) – 2013-04-10 10:15:03

0

使用垂直LinearLayout,而不是...

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_below="@id/imgLine" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 


<Button 
    android:id="@+id/btnLogin" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/action_via_email" 
    android:textColor="@android:color/white" 
    android:textStyle="bold" /> 

<Button 
    android:id="@+id/btnSignUp" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"    
    android:text="@string/action_sign_up" 
    android:textColor="@android:color/white" 
    android:textStyle="bold" /> 
</LinearLayout> 
+0

謝謝Nunu,但它顯示了兩個按鈕之間的一些空間。我不想要那個空間,這就是爲什麼我使用框架佈局,但我無法成功。 – 2013-04-10 07:29:13

+0

你可以通過使用重量和寬度爲0dp來控制兩個按鈕之間的空間。@ VK.Dev – k0sh 2013-04-10 07:33:58

+0

不,它不起作用。 – 2013-04-10 07:38:43

0

使用的FrameLayout你實際上是把一幅圖像上的另一個,而是使用的LinearLayout與按鍵寬度和高度包裹的內容,並避免使用按鈕的佈局權重。 讓我知道這是否可以解決您的問題

+0

謝謝Sangihta,不起作用,但它顯示了兩個按鈕之間的一些空間。我不想要那個空間,這就是爲什麼我使用框架佈局,但我無法成功。其實我想展示像分組的uitableview風格的ios。 – 2013-04-10 07:30:58

+0

好吧,然後嘗試添加填充到圖像之一,使圖像重疊後某些dpi – Sanghita 2013-04-10 07:33:36

+0

謝謝,但它不能在android 2.3.3中工作。是否有任何其他方式? – 2013-04-10 07:35:00

0

正如其他人所指出的,關鍵的答案是使用LinearLayout將按鈕放在一起。但是,對於「觸摸」效果,您需要執行以下一項或多項操作:

  1. 更改每個按鈕上的填充。
  2. 更改每個按鈕上的邊距。
  3. 更改按鈕背景圖像,使其右移到視圖區域的邊緣。

我更傾向於將到邊距和補設置爲零,設置圖像是你想要的效果:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_below="@id/imgLine" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <!-- options 1 and 2 --> 
    <Button 
     android:id="@+id/btnLogin" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/action_via_email" 
     android:textColor="@android:color/white" 
     android:padding="0dp" <!-- Set there to be no space between the content and the surrounding views --> 
     android:padding="0dp" <!-- Set to be no space int the content area but between the content and the edge of the content area --> 
     android:textStyle="bold" /> 

    <!-- option 3 --> 
    <Button 
     android:id="@+id/btnSignUp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"    
     android:text="@string/action_sign_up" 
     android:textColor="@android:color/white" 
     android:background="@drawable/my_background" 
     android:textStyle="bold" /> 
</LinearLayout> 

您需要定義my_background作爲想象繪製目錄或水庫。您可以從http://developer.android.com/guide/topics/graphics/2d-graphics.html的9個補丁映像部分獲得幫助。

0

使用TableLayout代替:

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

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" android:background="@drawable/button_style"/> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" android:background="@drawable/button_style"/> 

    </TableRow> 

</TableLayout> 

分配繪製button_style的按鈕:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<solid android:color="#38393B"/>  
<stroke android:width="0.5dp" android:color="#8aaa"/> 
<size android:width="120dp" android:height="80dp" /> 

+0

這應該是我接受的答案。適用於framelayout! – 2013-04-10 11:40:27

相關問題