簡體   English   中英

Android GridView無法正確縮放

[英]Android GridView Not Scaling Properly

我花了幾個小時在上面,研究了沒有解決方案就可以找到的每個相關的SO問題。

這是我的問題:

我有圖像的gridview。 我需要3列(以匹配iOS版本和美觀性)。 如果將numColumns設置為3,則每一行在圖像行的頂部和底部之間會得到很多額外的空間。 如果將寬度設置為自動調整,我總是得到2,它們看起來更好,但希望使用3列。

我想念什么?

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top" >

<LinearLayout 
    android:id="@+id/llayout"       
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    >
    <ImageView
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#666666"
        android:clickable="false"
        android:contentDescription="@string/title_card_collection"
        android:src="@drawable/sportscard2x" />
</LinearLayout>

<GridView
    android:id="@+id/cardGrid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_above="@+id/llayout"
    android:gravity="center"
    android:horizontalSpacing="2dip"
    android:numColumns="3"
    android:stretchMode="columnWidth"
    android:layout_alignParentBottom="true"
    android:verticalSpacing="2dip" >
</GridView>

屏幕截圖

我希望我只是想念這件事很容易。 提前致謝。

更新:添加了屏幕截圖。 問題是只顯示1行,如果向下滾動,則會看到其他3行,但是它們之間有很大的空間。

<編輯>

請在下面找到“提示3”。 帶有代碼的示例可以在這里找到。

</ edit>

我認為問題在於您將<GridView>layout_height設置為wrap_content 網格視圖似乎無法正確計算總高度,換行高度,因此,它僅顯示一行。

您可以將layout_height設置為match_parent (或任何其他固定高度-如120dp或其他值),或者可以嘗試擴展GridView Java對象並進行一些自己的計算(然后,您需要在您的自定義網格視圖對象中使用以及XML布局: <com.package.to.MyCustomGridView> )。

不過,我確實要強調一點,在API 11之前,沒有任何明確定義的方法可以從Java代碼中的網格視圖中獲取列數(您希望這樣做是為了計算數據的行數)適配器會產生)。 API 11中引入了getNumColumns 。沒有找到行之間間距的正確方法getVerticalSpacing() API 16中引入了getHorizontalSpacing()getVerticalSpacing()方法)。

供您參考: http : //developer.android.com/reference/android/widget/GridView.html

提示1:我還沒有對此進行自我測試,但是您可以嘗試執行以下操作:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...
        />

    <GridView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        ...
        />

</LinearLayout>

即在線性布局中包括網格視圖,並在布局圖像視圖后向其中添加所有可用空間。

提示2:您可以編寫自己的列表視圖。 不過,索尼移動教程網站上似乎有很多不錯的東西(不,我沒有受索尼移動公司雇用:-); 好教程就是好教程): http : //developer.sonymobile.com/2010/05/20/android-tutorial-making-your-own-3d-list-part-1/

提示3:您可以擴展Java GridView對象並重寫onMeasure方法,如下例所示。 記住要在Android布局XML文件中引用擴展的GridView類(例如<com.package.MyGridView android:layout_width="match_parent" android:layout_height="wrap_content" ... />

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightSpec;

    // The great Android "hackatlon" (in order to enable "wrap_content" on grid views).
    if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
        heightSpec = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    }
    else {
        heightSpec = heightMeasureSpec;
    }

    super.onMeasure(widthMeasureSpec, heightSpec);
}

希望您會在此找到一些啟發:-)

干杯-dbm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM