2013-06-23 97 views
0

我有Android水平LinearLayout与5图像。我希望5张图像在保持宽高比的同时水平放置屏幕。每张图片都是128x128像素。 问题在于,虽然宽度设置像什么,我想要的图像高度保持不变,所以他们看起来像这样一个更大的屏幕上(这是的Nexus 7):Android - 水平图像,以适应屏幕

enter image description here

我现在拥有的一切:

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:cropToPadding="true" 
     android:scaleType="fitXY" 
     android:src="@drawable/image01" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:scaleType="fitXY" 
     android:src="@drawable/image02" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:scaleType="fitXY" 
     android:src="@drawable/image03" /> 

    <ImageView 
     android:id="@+id/imageView4" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:scaleType="fitXY" 
     android:src="@drawable/image04" /> 

    <ImageView 
     android:id="@+id/imageView5" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:scaleType="fitXY" 
     android:src="@drawable/image05" /> 

</LinearLayout> 

感谢您的帮助

+0

你确定要调整大小 – MDMalik

回答

0

变化scaleType到centerInside或fitCenter和高度来匹配父。

+0

我试过了,但是Android并没有按比例缩放图像,即宽度保持不变,只有它们之间会有空格。我需要触摸图像。 – user2001951

+0

放大图像并保持宽高比并非如此简单。你可能需要手动完成。至于将图像触摸到ImageView边缘,只有centerCrop可以在保持宽高比的同时进行此操作。但是它会剪出图像的一部分,并且只会在图像大于图像视图时才会这样做,不要认为它适用于较小的图像。 –

+0

是的,我认为我会去使用linearlayout上的最小高度,并为每个屏幕尺寸使用不同的值资源。那就是我现在知道的解决方案。感谢您的答案btw。 – user2001951

0

尝试以环绕的ImageView与水平的LinearLayout

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:layout_gravity="center"> 

     <ImageView 
      android:id="@+id/btnHome" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:tag="btnhome" /> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:layout_gravity="center"> 
     <ImageView 
      android:id="@+id/btnGames" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:tag="btngames" /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:layout_gravity="center"> 
     <ImageView 
      android:id="@+id/btnNews" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:tag="btnnews"/> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:layout_gravity="center"> 
     <ImageView 
      android:id="@+id/btnPayment" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:tag="btnpayment"/> 
    </LinearLayout> 

然后,改变scaleType到fitCenter。

它会导致您的照片有一个正确的比例事件,您可以在更宽的屏幕中查看它。

0

我发现了一些代码来创建一个正方形认为我可以再使用,以使自己的自定义图形,您应该能够像这样:

public class SquareImageView extends ImageView { 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); 
     int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec); 
     int length = Math.min(width, height); 
     setMeasuredDimension(length, length); 
    } 
} 

我没有试过平方码在LinearLayout中,但是当使用RelativeLayout时,它首先被定位并附着到父母的三个尺寸上,它很好地工作。这是希望ImageView能够对图像做正确的事情,否则你将不得不重写onDraw()方法。