2011-11-05 58 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> 

您需要指定行数和列数。