【问题标题】:how to set image width height based on screen size in gridview如何在gridview中根据屏幕尺寸设置图像宽度高度
【发布时间】:2014-04-11 15:33:27
【问题描述】:

我想显示 3x3 大小的网格视图。我想根据设备大小设置高度和宽度。我正在参考this link

MainActivity-

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      GridView gridview = (GridView) findViewById(R.id.gridview);
      gridview.setAdapter(new ImageAdapter(this));
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

}

activity_main-

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
   android:id="@+id/gridview"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent"
   android:columnWidth="90dp"
   android:numColumns="auto_fit"
   android:verticalSpacing="10dp"
   android:horizontalSpacing="10dp"
   android:stretchMode="columnWidth"
   android:gravity="center"
/>

编辑-

就像首先获取屏幕高度和宽度,然后每个项目的高度和宽度是我得到的屏幕高度和宽度值的 1/3。

【问题讨论】:

  • 这里你的列是固定的 3
  • 我知道,但某些屏幕上网格视图中的图像仅占半屏以上。
  • 因为你设置了android:columnWidth="90dp",你需要在你的代码上处理它,正如我在之前的问题中所说的那样
  • 是的,但我想更改我的代码。我在平板电脑上检查我的代码。 Gridview 只占用半屏。
  • 在您的代码端设置宽度,获取屏幕宽度并将 width/3 设置为您的 gridView

标签: android gridview screen-size


【解决方案1】:

您可以获得如下屏幕尺寸:

final DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
final float screenWidthInDp=displayMetrics.widthPixels;
Log.WTF("ScreenWidth", "width: "+screenWidthInDp+", menuWidth: "+screenWidthInDp/3);

对于gridview,我建议你看看这个很棒的库Staggered Grid View.和他们的sample here.

【讨论】:

  • 我不想使用交错网格视图。如果我使用您建议的上述编码,我必须对上述代码进行哪些更改?
  • 不,事实并非如此。我在模拟器上试过这段代码。
【解决方案2】:

当我必须调整 Activitys 的大小时,我使用以下方法,我认为这对您的情况同样有效:

// I'm storing the size of the window in the display var, so I can then play around
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);

// In your case, you'll probably need something like this:
GridView gv = (GridView) findViewById(R.id.gridview);
gv.setWidth((int) size.x * 0.75);    // Whis would resize the grid width to the 75%
gv.setHeight((int) size.y * 0.5);    // Same with the height, but to the 50%

【讨论】:

  • 我为你删除了无用的行。在您的情况下,只需在填充 GridView 之前将这 6 行放入您的代码中,但是您可以通过它们的 id 找到它(我的意思是,一旦布局被渲染),只需将 % 更改为您需要的调整大小。它现在应该可以解决问题。
【解决方案3】:

不要使用屏幕尺寸,在多窗口上下文中,此方法无效。 如果您的网格是固定大小的 3x3 项目,请使用自定义布局 ViewGroup,如下所示:(并设置 RelativeLayout 项目内容)

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle state) {
        setContentView(new ViewGroup(this) {
            private RelativeLayout[] items = new RelativeLayout[9];
            private int width, height, itemWidth, itemHeight;
            {
                Random r = new Random();
                for (int i = 0; i < 9; i++) {
                    items[i] = new RelativeLayout(getContext());
                    float[] hsv = new float[] {360 * r.nextFloat(), .50f, .75f};
                    items[i].setBackgroundColor(Color.HSVToColor(hsv));
                    addView(items[i]);

                    // UPDATE ////////////////////////////////////
                    ImageView image = new ImageView(getContext());
                    switch (i) {
                    case 0: // top left
                    case 1: // top center
                    case 2: // top right
                    case 3: // center left
                    case 4: // center center
                    case 5: // center right
                    case 6: // bottom left
                    case 7: // bottom center
                    case 8: // bottom right
                        image.setImageResource(R.drawable.ic_launcher);
                        break;
                    }
                    image.setScaleType(ScaleType.FIT_XY);
                    image.setLayoutParams(new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.MATCH_PARENT,
                            RelativeLayout.LayoutParams.MATCH_PARENT
                    ));
                    items[i].addView(image);
                    //////////////////////////////////////////////
                }
            }
            @Override
            protected void onMeasure(int wMS, int hMS) {
                width = MeasureSpec.getSize(wMS);
                height = MeasureSpec.getSize(hMS);
                itemWidth = width / 3;
                itemHeight = height / 3;
                wMS = MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY);
                hMS = MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY);
                measureChildren(wMS, hMS);
                setMeasuredDimension(width, height);
            }
            @Override
            protected void onLayout(boolean changed, int l, int t, int r, int b) {
                for (int i = 0; i < 9; i++) {
                    l = itemWidth * (i % 3);
                    t = itemHeight * (i / 3);
                    r = l + itemWidth;
                    b = t + itemHeight;
                    items[i].layout(l, t, r, b);
                }
            }
        });
        super.onCreate(state);
    }

}

编辑:查看我的代码更新,您只需将图像添加到项目容器中即可。使用这种方法,不需要 XML 布局文件,因为您自己管理内容和大小。

结果


编辑:极简方式:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle state) {
        setContentView(new ViewGroup(this) {
            private static final int SIZE_X = 3;
            private static final int SIZE_Y = 3;
            private ImageView[] items = new ImageView[SIZE_X * SIZE_Y];
            private int itemWidth, itemHeight;
            {
                setBackgroundColor(Color.DKGRAY);
                for (int i = 0; i < items.length; i++) {
                    items[i] = new ImageView(getContext());
                    items[i].setScaleType(ScaleType.CENTER);
                    items[i].setImageResource(R.drawable.ic_launcher);
                    addView(items[i]);
                }
            }
            @Override
            protected void onMeasure(int wMS, int hMS) {
                int width = MeasureSpec.getSize(wMS);
                int height = MeasureSpec.getSize(hMS);
                itemWidth = width / SIZE_X;
                itemHeight = height / SIZE_Y;
                wMS = MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY);
                hMS = MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY);
                measureChildren(wMS, hMS);
                setMeasuredDimension(width, height);
            }
            @Override
            protected void onLayout(boolean changed, int l, int t, int r, int b) {
                for (int i = 0; i < items.length; i++) {
                    l = itemWidth * (i % SIZE_X);
                    t = itemHeight * (i / SIZE_X);
                    r = l + itemWidth;
                    b = t + itemHeight;
                    items[i].layout(l, t, r, b);
                }
            }
        });
        super.onCreate(state);
    }

}

结果


@坎瓦尔吉特·辛格:

在 MainActivity 项目创建循环中:

final int id = i;
items[i].setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(getContext(), NextActivity.class).putExtra("id", id));
    }
});

在 NextActivity 中:

int id = getIntent().getIntExtra("id", -1);

【讨论】:

  • 只需将此代码复制/粘贴到您的项目中并根据您的需要进行调整
  • 你完全改变了我的代码。如果我必须将图像的价值发送到我可以执行此操作的下一个活动,请帮助我做一件事。例如,在第一张图片上单击将 0 发送到下一个活动。
  • @alex 它的工作。最后一个问题如何在行和列中设置垂直和水平填充?
  • @JohnR 如果它正在工作......你可以批准亚历克斯的回答对吗? :-)
  • @Jagadeesh 当然。但是在得到我最后一个问题的答案之后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
  • 2013-10-09
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多