2012-11-30 149 views
2

我试图让三个按钮彼此相邻,左边的一个和右边的一个是小的,中间的一个应该填满宽度的其余部分 - 2x2dp(= margin) 。 我对以下代码进行了编码,但它不断推动我的右键离开屏幕,关于如何解决此问题的任何想法?可能有办法让左边的和右边的都有效?谢谢!RelativeLayout:按钮将其他按钮从屏幕上移开

<RelativeLayout 
      android:id="@+id/rl" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="10dp" > 

      <Button 
       android:id="@+id/bPreviousQuestion" 
       android:layout_width="42dp" 
       android:layout_height="42dp" 
       android:background="#c8c8c8" 
       android:text="&lt;" 
       android:textColor="#ffffff" 
       android:textSize="20dp" 
       /> 

      <Button 
       android:id="@+id/bBack" 
       android:layout_width="fill_parent" 
       android:layout_height="42dp" 
       android:layout_toRightOf="@+id/bPreviousQuestion" 
       android:background="#c8c8c8" 
       android:text="Go Back" 
       android:textColor="#ffffff" 
       android:textSize="20dp" 
       android:layout_marginLeft="2dp" 
       android:layout_marginRight="2dp" /> 

      <Button 
       android:id="@+id/bNextQuestion" 
       android:layout_width="42dp" 
       android:layout_height="42dp" 
       android:layout_toRightOf="@+id/bBack" 
       android:background="#c8c8c8" 
       android:text="&gt;" 
       android:textColor="#ffffff" 
       android:textSize="20dp" /> 
     </RelativeLayout> 

回答

3

尝试增加:

android:layout_toLeftOf="@+id/bNextQuestion" 

你的 “背”按钮。在“下一个”删除此行:

android:layout_toRightOf="@+id/bBack" 

并添加:

android:layout_alignParentRight="true" 
+0

不错的山姆,谢谢!所以你确实可以通过让它们取决于你在屏幕上“修复”的其他按钮来实现按钮的特权,谢谢! –

+2

是的,通过试验和错误,你会发现哪些属性优先于其他属性。例如,'layout_alignParent ____'是一个将视图固定到某个位置的更加明确的属性,而'toLeftOf'和'toRightOf'则是动态设计。你还会发现诸如'layout_center _____'这样的属性不会改变,它们将以其特定的轴为中心。 – Sam

0

你的第二按钮的layout_width是 “FILL_PARENT”

+0

是的,我知道,那就是推动它,但我可以采用某种特权的左,右一个抵靠中间按钮?还是有另一种方式? –

2

试试这个:

集合{}上左与42dp宽度对准父母。

将{Next}设置为对齐的父权,宽度为42dp。

将{Back}设置为位于Prev的右侧,并且设置为具有fill_parent宽度的Next的左侧。

应该如果我正确地正确布局布局,用后退按钮填充按钮。

+1

感谢马特,与山姆一样的解决方案,它的工作原理! :) –

0

你的第二个按钮是中间按钮,你正在使用android:layout_width =“fill_parent”。取而代之的是采用Android的:layout_width = “42dp”,然后你会看到你的第三个

+0

是的,但取决于屏幕的大小,按钮看起来很奇怪。如果理解正确,OP将希望看到后退按钮被拉伸以填充Prev和Next按钮之间的空间。 –

+0

正确的马特,这是我需要完成 –

+0

然后你需要使用相对布局内的线性布局放置在底部。在线性布局设置布局:所有三个按钮的重量 –

1

使用LinearLayout代替RelativeLayout

<LinearLayout 
    android:id="@+id/rl" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="10dp" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/bPreviousQuestion" 
     android:layout_width="42dp" 
     android:layout_height="42dp" 
     android:background="#c8c8c8" 
     android:text="&lt;" 
     android:textColor="#ffffff" 
     android:textSize="20dp" /> 

    <Button 
     android:id="@+id/bBack" 
     android:layout_width="fill_parent" 
     android:layout_height="42dp" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="2dp" 
     android:layout_weight="1" 
     android:background="#c8c8c8" 
     android:text="Go Back" 
     android:textColor="#ffffff" 
     android:textSize="20dp" /> 

    <Button 
     android:id="@+id/bNextQuestion" 
     android:layout_width="42dp" 
     android:layout_height="42dp" 
     android:background="#c8c8c8" 
     android:text=">" 
     android:textColor="#ffffff" 
     android:textSize="20dp" /> 
</LinearLayout> 
+0

我需要相对布局将它放在底部...... :) –

+0

'LinearLayout'可以放在底部! –