2013-04-10 50 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