2011-11-05 61 views
0

我想擁有圖像視圖的網格類型佈局。開始我試圖看看我是否可以使用簡單的按鈕來獲得這個。 這很容易,我可以使用線性佈局創建一組按鈕,但是當我嘗試使用imageView而不是按鈕時出現問題。
有沒有一種簡單的方法來強制android以適合imageView在一個按鈕適合的完全相同的空間。
圖像視圖似乎忽略寬度的重量?我不介意圖片的高寬比是否會變形,但理想情況下,它會保持這個比例,只是縮小圖像直到其完全吻合。什麼是最好的方式來得到這個工作?試圖獲取排列在網格類型佈局中的圖像視圖

這是我當前XML .......

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

<LinearLayout 
    android:id="@+id/linearLayout3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/chicken" /> 

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

</LinearLayout> 
+1

嘗試爲您的ImageView使用android:scaleType =「fitXY」。 –

回答

0

,如果你在你的LinearLayout使用重量/ weightsum參數,你可能需要設置你的身高(如果LinearLayout中是垂直的)或寬度(如果linearlayout是水平的)爲0dip而不是fill_parent。這樣你的layout_weight分佈將被正確計算。如果你使用相同的權重(1),這並不重要,但是當你嘗試爲線性佈局中的不同孩子分配不同的權重時,你會注意到這種差異。

用於圖像瀏覽,你可以看看這link爲不同的imageview scaletypes。你將需要FIT_XY來達到這個特定的目的。

+0

是的,這對我有效。這個例子與我目前正在閱讀的書「開始Android開發」非常相似。我很驚訝沒有更簡單的方法來實現這樣的UI,而不是以編程方式實現它。 – MayoMan

0

您可以嘗試使用來自Romain Guy的photostream應用程序中的網格佈局。它根據行和列的數量在相同大小的單元格中設置子視圖。 Here is source code

它看起來像這樣的xml文件

<com.blessan.dashboard.GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <!--Child views go here--> 
</com.blessan.dashboard.GridLayout> 

您需要指定行數和列數。