2011-01-28 117 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