2010-09-03 52 views
1

我真的在我的應用程序的佈局的一部分掙扎。我有一個帶有居中圖像的標題欄,右側應該有兩個按鈕(或者像我現在擁有的可點擊圖像)。我設法讓彼此旁邊的圖像(都居中),但我想要在右邊的圖像。當我使用layout_weight創建「第一個」拉伸時,按鈕位於右側,但它們非常小。我希望按鈕(圖像)保持其原始大小。我甚至試圖使用minWidth和minHeight對其進行硬編碼,但這不起作用。Android:酒吧與中心圖像和按鈕在右邊

這裏是中心所有代碼:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:background="@drawable/header_tile"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/header_logo"> 
    </ImageView> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:id="@+id/previous" 
      android:src="@drawable/omhoog" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
     </ImageView> 

     <ImageView 
      android:id="@+id/next" 
      android:src="@drawable/omlaag" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
     </ImageView> 
    </LinearLayout> 
</LinearLayout> 

這裏是這樣的作品,除了最後的圖像變得如此小的代碼:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:background="@drawable/header_tile"> 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/header_logo" 
     android:layout_weight="2"> 
    </ImageView> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     <ImageView 
      android:id="@+id/previous" 
      android:src="@drawable/omhoog" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
     </ImageView> 

     <ImageView 
      android:id="@+id/next" 
      android:src="@drawable/omlaag" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
     </ImageView> 
    </LinearLayout> 
</LinearLayout> 

任何想法?非常感謝!

埃裏克

回答

1

使用相對佈局,它是更強大,請注意layout_centerInParent和layout_alignParentRight屬性:

<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/header_tile"> 
    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/header_logo" 
     android:layout_centerInParent="true"> 
    </ImageView> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true"> 
     <ImageView 
      android:id="@+id/previous" 
      android:src="@drawable/omhoog" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
     </ImageView> 
     <ImageView 
      android:id="@+id/next" 
      android:src="@drawable/omlaag" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
     </ImageView> 
    </LinearLayout> 
</RelativeLayout> 
+0

工程很好,直到現在還不知道RelativeLayout。完善! – Erik 2010-09-03 09:25:51

0

與FILL_PARENT & WRAP_CONTENT使用時layout_weight的工作方式不同;

  • WRAP_CONTENT:它試圖擴大在權重的比率中的項目,以便覆蓋整個空間

  • FILL_PARENT:它試圖壓縮在權重的比率的項目。

所有視圖使用權重

時必須具有相同的類型(FILL_PARENT/WRAP_CONTENT)
0

的xmlns:機器人= 「http://schemas.android.com/apk/res/android」>

<LinearLayout android:id="@+id/LinearLayout01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center"> 
    <Button android:text="@+id/Button03" 
     android:id="@+id/Button03" 
     android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:layout_width="wrap_content"> 
    </Button> 
</LinearLayout> 

<LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content">  
    <Button android:id="@+id/Button01" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="/\"> 
     </Button> 
    <Button android:id="@+id/Button02" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="\/"> 
     </Button> 
</LinearLayout> 

不知道你的圖像的大小,所以我使用的按鈕。重量值可以是2嗎? 0或1我總是使用。