2016-04-22 114 views
0

我們已經構建了一個在線購物應用程序。Android Grdiview顯示不正確

我們有兩個gridview的佈局。 第一個:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="0dp" 
android:paddingRight="0dp" 
android:paddingTop="0dp" 
android:paddingBottom="0dp" 
tools:context=".HomeActivity"> 
<GridView 
    android:id="@+id/gridView" 
    android:numColumns="3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
</RelativeLayout> 

,第二個用於每個gridview的細胞:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/singleCell" 
android:layout_width="345px" 
android:layout_height="400px" 
android:background="#E6E6E6"> 

<ImageView 
    android:contentDescription="@string/hello_world" 
    android:id="@+id/profileImg" 
    android:layout_width="fill_parent" 
    android:layout_height="330px" 
    android:scaleType="fitXY" 
    android:layout_marginLeft="1px" 
    android:layout_marginTop="2px"/> 

<TextView 
    android:id="@+id/friend_name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:text="Name" 
    android:textColor="#000000" 
    android:textStyle="bold" 
    android:layout_marginTop="340px" 
    android:layout_marginLeft="20px" 
    android:textSize="13sp"> 
</TextView> 

</FrameLayout> 

的問題是,在一些器件中,gridview的正確顯示像這樣,比例是確定。

gridview is displayed correctly on some devices

但在其它幾個裝置,被不正確地顯示的圖像的高度和比的變化和圖像。

gridview is displayed incorrectly on some devices

如果你給我們一個解決方案,幫助我們,我們會很高興。這是一個關鍵問題,因爲網格單元圖像高度在某些設備中變化並變得更長。

回答

0

您可以嘗試改變Imageview的縮放類型。在此線

android:scaleType="centerCrop" 

代替

android:scaleType="fitXY" 
+0

Thanks @ nazmul-hasan-masum。我們測試你的解決方案現在比例更好,但問題在於,圖像左側和右側的一部分不會顯示在之前存在問題的設備中。將[舊版本](http://i.stack.imgur.com/uWKTx.jpg)與[您提供的解決方案](http://uploads.im/BH9SY.jpg)進行比較。正如你所看到的,現在圖像比例看起來更好,但是左右的每個單元格的一部分都會被裁剪掉,並且它不應該被裁剪。 –

0

@納茲穆爾 - 哈桑 - masum,謝謝。我用你的解決方案,但起初問題沒有得到解決,所以我將imageview和文本單元從px更改爲等效的dp,現在一切正常。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/singleCell" 
    android:layout_width="345px" 
    android:layout_height="400px" 
    android:background="#E6E6E6"> 

    <ImageView 
     android:contentDescription="@string/hello_world" 
     android:id="@+id/profileImg" 
     android:layout_width="fill_parent" 
     android:layout_height="128dp" 
     android:scaleType="centerCrop" 
     android:layout_marginLeft="1px" 
     android:layout_marginRight="1px" 
     android:layout_marginTop="2px"/> 

    <TextView 
     android:id="@+id/friend_name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Name" 
     android:textColor="#000000" 
     android:textStyle="bold" 
     android:layout_marginTop="130dp" 
     android:layout_marginLeft="20px" 
     android:textSize="13sp"> 
    </TextView> 

</FrameLayout>