2011-02-24 118 views
1

我想准备一个与下面附加图像相同的布局。android-问题在布局

我的问题都表示在图片:

  1. 我尝试建立按钮完全相同(宽度)在图像
  2. 表示为1我尝试设置“保存”和“取消“按钮在下面等宽。

enter image description here

我曾与下面的XML布局试了一下:

<RelativeLayout 
    android:id="@+id/widget38" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:padding="5dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:id="@+id/textView1" 
     android:text="Event" 
     android:layout_alignParentLeft="true" 
     android:layout_height="wrap_content"> 
    </TextView> 

    <EditText 
     android:layout_width="fill_parent" 
     android:id="@+id/editText1" 
     android:layout_alignParentLeft="true" 
     android:hint="Tap to enter title" 
     android:layout_marginTop="5dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1"> 
    </EditText> 

    <View 
     android:id="@+id/separator"   
     android:background="#ffffff"   
     android:layout_width = "fill_parent" 
     android:layout_height="1dip" 
     android:layout_centerVertical ="true" 
     android:layout_below="@+id/editText1"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:id="@+id/textView2" 
     android:text="From" 
     android:layout_alignParentLeft="true" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/separator" 
     android:layout_marginTop="5dp"> 
    </TextView> 

    <Button 
     android:layout_width="wrap_content" 
     android:id="@+id/button1" 
     android:layout_alignParentLeft="true" 
     android:text="Button" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView2"> 
    </Button> 

    <Button 
     android:layout_width="wrap_content" 
     android:id="@+id/button2" 
     android:text="Button" 
     android:layout_marginLeft="10dp" 
     android:layout_alignTop="@+id/button1" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/button1" 
     android:layout_below="@+id/button1"> 
    </Button> 

    <View 
     android:id="@+id/separator1"   
     android:background="#ffffff"   
     android:layout_width = "fill_parent" 
     android:layout_height="1dip" 
     android:layout_centerVertical ="true" 
     android:layout_below="@+id/button2"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:id="@+id/textView3" 
     android:text="To" 
     android:layout_alignParentLeft="true" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/separator1" 
     android:layout_marginTop="5dp"> 
    </TextView> 

    <Button 
     android:layout_width="wrap_content" 
     android:id="@+id/button3" 
     android:layout_alignParentLeft="true" 
     android:text="Button" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView3"> 
    </Button> 

    <Button 
     android:layout_width="wrap_content" 
     android:id="@+id/button4" 
     android:text="Button" 
     android:layout_marginLeft="10dp" 
     android:layout_alignTop="@+id/button3" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/button3" 
     android:layout_below="@+id/button3"> 
    </Button> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/relativeLayout1" 
     android:layout_alignParentBottom="true"> 

     <Button android:layout_width="wrap_content" android:id="@+id/button5" android:layout_alignParentBottom="true" android:text="Save" android:layout_height="wrap_content"></Button> 
     <Button android:layout_width="wrap_content" android:id="@+id/button6" android:layout_alignParentBottom="true" android:text="Cancel" android:layout_height="wrap_content" android:layout_toRightOf="@+id/button5"></Button> 
    </RelativeLayout> 


</RelativeLayout> 
+4

我不知道你*问题*是什么。 – SK9 2011-02-24 14:21:58

+1

他希望他的布局与图像中的布局相匹配。似乎对我很直接。 – 2011-02-24 14:25:39

+0

对于OP,如果您可以发布由布局XML生成的布局图像,将会很有帮助。 – 2011-02-24 14:26:18

回答

1

您可以通过使用android:layout_weight

这里调整重量一个按钮,是一个如何使用它的一个例子: http://android.amberfog.com/?p=328

+0

我在下面的“保存/取消”按钮的想法,我已经设置宽度为0dp,然后重量= 1的两个按钮。 – 2011-02-25 05:58:16

+0

Thanx的大支持,现在让我再次尝试上部bottons问题 – 2011-02-25 05:58:50

+0

Thanx很多,对于上部按钮,我已经完成与android:layout_width =“wrap_content”为两个按钮,和android:layout_weight =“1”为第一个按钮,旁边的按钮(右侧)为“0”。 – 2011-02-25 06:37:33

0

你可以只使用水平线性布局。对于第一个问题,你应该为你的.xml布局文件按钮设置“权重”值

1

标有“图像1”的按钮,可以设置一个固定的宽度,如“16dip”即

<Button 
     android:layout_width="16dip" 
     android:id="@+id/button1" 
     android:layout_alignParentLeft="true" 
     android:text="Button" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView2"> 
    </Button> 

为按钮下可以添加在到一个LinearLayout水平方向,0宽度和一个重量值

<Button 
      android:layout_width="0dip" 
      android:layout_weight="1" 
      android:id="@+id/button4" 
      android:text="Button" 
      android:layout_marginLeft="10dp" 
      android:layout_alignTop="@+id/button3" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/button3" 
      android:layout_below="@+id/button3"> 
     </Button> 
+0

+1,thanx支持,我得到了解决方案,它的工作正常,我有“保存/取消”按钮在底部栏等长。现在,我要为您的解决方案尝试上述按钮。请更正上面的代码:由于按钮ID是“button5”而不是“button4” – 2011-02-25 06:07:16

+0

但我试图将上面的按钮固定为“200dp”和“50dp”。它看起来很好,并在小屏幕上很好地安装。但是当我在大屏幕上测试它时,右侧区域会变空。所以请帮助我。 – 2011-02-25 06:26:14

+0

对于上面的按钮,我从@Phonon提供的链接中得到了这个想法,因为我已经为android和layout两个按钮使用了android:layout_width =“wrap_content”,而第一个按钮和android:layout_weight =“1”为第二个按钮。 – 2011-02-25 06:36:49

0

我认为你的问题是,你有不一样的XML代码?

必须使用android:layout_weight

,如果你想按钮采取同样的地方(取消和有效的按钮上的图片),你必须把android:layout_weight="1"两个例如

0

在活动的onCreate()方法设置内容视图后执行以下操作。

findViewById(R.id.button1).setWidth(findViewById(R.id.button5).getWidth()); 
findViewById(R.id.button2).setWidth(findViewById(R.id.button6).getWidth()); 

并让我知道这是否有效。