1

我一直在玩一些XML以達到以下目的;在linearlayout中使用可用空間拉伸imagebuttons

第一行是橫幅(正常工作)。然後;每行有3個按鈕。我希望按鈕佔據所有的水平空間(所以屏幕寬度由3改爲)。此外,按鈕上的圖像應該調整到這個寬度,而不改變縱橫比!如何才能做到這一點?

接下來;如果我添加ImageButtons行,我應該如何在滾動視圖中「殼」LinearLayouts以維護3個按鈕的寬度分佈?

日Thnx

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerHorizontal="true" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/blogbanner" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="fitXY" 
     android:src="@drawable/test_banner_ad" /> 

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

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      android:src="@drawable/trololo" /> 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:scaleType="matrix" 
      android:src="@drawable/scout" /> 

     <ImageButton 
      android:id="@+id/imageButton3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:scaleType="fitEnd" 
      android:src="@drawable/painting" /> 

    </LinearLayout> 

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

     <ImageButton 
      android:id="@+id/imageButton4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/whoop" /> 

     <ImageButton 
      android:id="@+id/imageButton5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/drown" /> 

     <ImageButton 
      android:id="@+id/imageButton6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/love" /> 
    </LinearLayout> 



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

     <ImageButton 
      android:id="@+id/imageButton7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/shredder" /> 

     <ImageButton 
      android:id="@+id/imageButton8" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/frog" /> 

     <ImageButton 
      android:id="@+id/imageButton9" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/yeah03" /> 
    </LinearLayout> 
</LinearLayout> 

回答

1

將每行的3個按鈕到另一個水平佈局,然後用 「」 屬性來定義它們的寬度。

圖片將需要有尺寸「fill_parent」&使用scaleXY

+0

我是否爲寬度和高度選擇填充父項?或者其中之一?..我正在嘗試,但圖像不保持寬高比,它們被拉伸..還有,scaleXY不存在?現在使用FitXY,但它延伸圖像.. –

+0

是的,兩者。 對不起,我以爲你想讓他們伸展。要保持比例使用: android:scaleType =「CENTER_INSIDE」 或 android:scaleType =「CENTER_CROP」 – bhekman

+0

請參閱此處瞭解更多信息:http://developer.android.com/reference/android/widget/ImageView.ScaleType html的 – bhekman

相關問題