Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-GridView调整单元格内容大小_Android_Android Layout_Gridview - Fatal编程技术网

Android-GridView调整单元格内容大小

Android-GridView调整单元格内容大小,android,android-layout,gridview,Android,Android Layout,Gridview,我试图构建一个GridView,其中所有单元格都具有相同的高度,但GridView似乎对每个单元格应用了不同的高度。我在单元格中膨胀的布局上,将布局高度设置为一个固定的度量值,如果我只是将其添加到静态活动中,则会保持该度量值。但当我试图在单元格中充气时,它似乎在将布局调整到所需的最小空间 布局XML: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_wid

我试图构建一个GridView,其中所有单元格都具有相同的高度,但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


我对安卓相当陌生,所以这可能是一个需要解决的小问题。我很欣赏任何输入:)

在GridView中显示项目时,我会将整个网格项目视图包装在固定的aspect 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);
        }
}

您可以尝试设置最小高度。或者,由于希望实现固定高度,因此可以为线性布局内的视图设置高度。这取决于相对布局的复杂性。。