2015-03-19 68 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); 
     } 
} 
相关问题