2011-01-28 147 views
1

在我的项目中,我有一个4按钮的视图。我有一个相对布局来正确放置它们。问题是我想让按钮填满整个屏幕而不会消失,我不想为它们设置固定大小以确保按钮适合任何屏幕大小。现在我唯一能看到的就是一个大按钮,因为RelativeLayout没有android:layout_weight。我怎样才能做到这一点?这是我得到:按钮大小调整问题。 Android

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

      <Button 
       android:id="@+id/search_icon" 
       android:background="@drawable/search_icon" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       /> 

      <Button 
       android:id="@+id/categories_icon" 
       android:background="@drawable/categories" 
       android:layout_toRightOf="@+id/search_icon" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       /> 

      <Button 
       android:id="@+id/green_icon" 
       android:background="@drawable/green_icon" 
       android:layout_below="@+id/search_icon" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       /> 

      <Button 
       android:id="@+id/red_icon" 
       android:background="@drawable/red_icon" 
       android:layout_toRightOf="@+id/green_icon" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       /> 

    </RelativeLayout> 

回答

2

好,似乎比我想象的更容易...我想你正在寻找这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:layout_> 

    <Button android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="1" /> 

    <Button android:id="@+id/button2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="2" /> 
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" > 

    <Button android:id="@+id/button3" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="3" /> 

    <Button android:id="@+id/button4" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="4" /> 
</LinearLayout> 

试一下,它应该看起来像这样:

Android screen with 4 buttons

0

有关RelativeLayout的事情是,你必须告诉在哪里把itens。示例:

<Button android:id="@+id/buyTicket" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Buy ticket" 
    android:layout_above="@id/LinearLayout02" /> 

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

请注意android:layout_above =“@ id/buyTicket”。 如果你想在按钮B顶部的按钮A,你首先声明按钮A(例如),将其对齐到父项的顶部,然后在按钮B声明,你说你想要它在按钮A下。

在你的情况下,你必须声明按钮A,B,C和D,然后在按钮A中设置android:layout_alignParentTop="true",然后在B,C和D(在它们之前)设置android:layout_bellow。不要忘记将高度和宽度设置为wrap_content

+0

我照你说的做了,但没有奏效。我会再次发布代码 – 2011-01-28 15:48:29

+0

试试我今天发布的代码。我这样做:http://3.ly/UXg2 – gnclmorais 2011-01-29 20:47:17

1

这是你在找什么? enter image description here

这里是我做此布局的代码:

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

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
     <Button 
      android:text="BUTTON 1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      /> 

     <Button 
      android:text="BUTTON 2" 
      android:layout_toRightOf="@+id/search_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
     <Button 
      android:text="BUTTON 3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      /> 

     <Button 
      android:text="BUTTON 4" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      /> 
</LinearLayout> 
</LinearLayout> 
+0

如果你设置fill_parent的一切,那么整个屏幕上只有4个按钮。这是对上述问题的回答。 – Lumis 2011-01-28 16:04:26