2015-03-19 70 views
0

我正在嘗試構建一個GridView,其中所有單元格都具有相同的高度,但看起來GridView對每個單元格應用了不同的高度。我將layout_height設置爲在單元格中膨脹的佈局上的固定尺寸,如果我只是將其添加到靜態活動中,則這會保持不變。但是,當我嘗試在單元中膨脹它時,它似乎正在將佈局調整到所需的最小空間。Android- GridView調整單元格內容大小

佈局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="295dp" 
    android:layout_height="350dp" 
    android:orientation="vertical" 
    android:background="@drawable/row_book_back" > 

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

    <ImageView 
     android:id="@+id/img_thumbnail" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" 
     android:layout_alignParentLeft="true" 
     android:scaleType="center" 
     android:src="@drawable/sample_image" /> 

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

所以我有有效的頁眉和頁腳,並應在剩餘空間爲中心的圖像。當我將它添加到單元格上時會發生什麼情況,圖像佔用的空間會根據圖像高度調整大小,而不是隻將圖像居中,整個佈局最終會少於指定的350dp。

我對Android很新,所以這可能是一個微不足道的問題。我很欣賞任何輸入:)

+0

您可以嘗試設置minHeight。或者因爲你想達到固定的高度,你可以將高度設置爲LinearLayout中的視圖。真的取決於你的相對佈局的複雜性。 – Lamorak 2015-03-19 12:58:08

回答

0

當在GridView中顯示項目時,我會將整個網格項目視圖封裝在一個固定的FrameLayout方面。我也會將ImageView封裝在同一個佈局中以修復它的大小。我一直在使用它來處理需要固定在16:9或4:3的視頻剪輯,你的情況可能會有所不同。調整外部佈局的外觀以適應頁眉和頁腳的大小。

我對此代碼沒有任何要求,在某處遇到它,但我必須添加樣式屬性,以便可以在xml中設置方面。

public class FixedAspectFrame extends FrameLayout { 

    public FixedAspectFrame(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setStyledAttributes(context, attrs);   
     mContext = context; 

    } 

    private float mAspectRatio = 1.7777f; // 16:9 
    private Context mContext = null; 

    private void setStyledAttributes(Context context, AttributeSet attrs) { 
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FixedAspectFrame); 
     mAspectRatio = a.getFloat(R.styleable.FixedAspectFrame_aspect, mAspectRatio); 

     a.recycle(); 
    } 

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

      int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
      int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
      int receivedWidth = MeasureSpec.getSize(widthMeasureSpec); 
      int receivedHeight = MeasureSpec.getSize(heightMeasureSpec); 

      int measuredWidth; 
      int measuredHeight; 
      boolean widthDynamic; 
      if (heightMode == MeasureSpec.EXACTLY) { 
       if (widthMode == MeasureSpec.EXACTLY) { 
       widthDynamic = receivedWidth == 0; 
       } else { 
       widthDynamic = true; 
       } 
      } else if (widthMode == MeasureSpec.EXACTLY) { 
       widthDynamic = false; 
      } else { 
       super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
       return; 
      } 
      if (widthDynamic) { 
       // Width is dynamic. 
       int w = (int) (receivedHeight * mAspectRatio); 
       measuredWidth = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY); 
       measuredHeight = heightMeasureSpec; 
      } else { 
       // Height is dynamic. 
       measuredWidth = widthMeasureSpec; 
       int h = (int) (receivedWidth/mAspectRatio); 
       measuredHeight = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); 
      } 
      super.onMeasure(measuredWidth, measuredHeight); 
     } 
} 
相關問題