【问题标题】:Android- GridView resizing cell contentsAndroid- GridView 调整单元格内容的大小
【发布时间】:2015-03-19 22:11:28
【问题描述】:

我正在尝试构建一个所有单元格都具有相同高度的 GridView,但似乎 GridView 正在对每个单元格应用不同的高度。我将 layout_height 设置为我在单元格中膨胀的布局上的固定度量,如果我只是将它添加到静态活动中,这将保持不变。但是当我尝试在单元格中充气时,它似乎正在将该布局调整为所需的最小空间。

布局 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 还很陌生,所以这可能是一个需要解决的小问题。我感谢任何意见:)

【问题讨论】:

  • 您可以尝试设置 minHeight。或者,由于您想实现固定高度,您可以将高度设置为 LinearLayout 内的视图。真的取决于你的相对布局的复杂性..

标签: android android-layout gridview


【解决方案1】:

当在 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);
        }
}

【讨论】:

    猜你喜欢
    • 2013-05-26
    • 2015-10-03
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-09
    • 2017-02-28
    相关资源
    最近更新 更多