(Android - xml)Gridview和ImageView缩放类型为centerCrop

时间:2013-03-26 13:47:14

标签: android android-layout gridview imageview crop

我正在尝试在GridView中显示图像列表,我想仅裁剪此列表中图像的中心。以下是每个图像项的布局:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/place_image"
        android:src="@drawable/ic_res"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="centerCrop"/>

这是包含网格视图的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout android:id="@+id/upload_view"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:background="#f6f6f6"
                  android:padding="10dp"
                  android:visibility="gone">
        <LinearLayout android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:orientation="horizontal">
            <ImageButton android:id="@+id/photo_select"
                    android:layout_width="50dp"
                    android:layout_height="40dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/ic_photo"/>
            <EditText android:id="@+id/photo_caption"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:hint="@string/caption"
                      android:inputType="textMultiLine"/>
        </LinearLayout>
        <TextView  android:id="@+id/photo_path"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="" />

        <Button android:id="@+id/photo_upload"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/upload_photo"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:layout_gravity="right"/>
    </LinearLayout>
    <TextView android:id="@+id/no_photos"
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:gravity="center"
              android:textStyle="bold"
              android:textSize="20dp"
              android:text="@string/no_photos"
              android:visibility="gone" />
    <GridView android:id="@+id/grid_photos"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="20dp"
            android:columnWidth="120dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="10dp"
            android:horizontalSpacing="10dp"
            android:stretchMode="columnWidth"
            android:gravity="center" />
</LinearLayout>

这是我得到的结果:

enter image description here

这很奇怪,因为图像都是以这种奇怪的方式裁剪的。以下是我原始图片之一:

enter image description here

我从未遇到过这个问题,所以如果你们中的任何人都熟悉这个问题,请帮我找到原因。谢谢!

P.s:我想这只能是xml布局的问题所以我认为没有必要在这里包含我的android源代码。但如果确实如此,请告诉我问题的来源,我会用我的代码编辑这个问题。

1 个答案:

答案 0 :(得分:3)

原因是GridView清除了为磁贴设置的所有布局参数,并设置了自己的布局参数,因此您可以在ImageView内添加RelativeLayout。这与所有AdapterView类似,如ListView,Gallery等。

相关问题