简体   繁体   English

确保尺寸是屏幕尺寸的一个因素

[英]Ensuring a size is a factor of the screen size

I have a GridView in Android which objects change size during a screen rotation. 我在Android中有一个GridView,该对象在屏幕旋转时会更改大小。 I also want there to be no gaps in between the objects. 我还希望这些对象之间没有间隙。 I want each View to be around 150-200dp. 我希望每个视图都在150-200dp左右。

At first, I was thinking that I could just get the total screen width ( int totalWidth = getWindowManager().getDefaultDisplay().getWidth(); ) and use something like m_IconSize = Math.min(totalWidth / 4, 175); 起初,我以为我可以获取屏幕的总宽度( int totalWidth = getWindowManager().getDefaultDisplay().getWidth(); ),然后使用类似m_IconSize = Math.min(totalWidth / 4, 175); to get the max icon size, then set the grid view's columns with gridView.setNumColumns(totalWidth / m_IconSize); 要获得最大图标大小,然后使用gridView.setNumColumns(totalWidth / m_IconSize);设置网格视图的列gridView.setNumColumns(totalWidth / m_IconSize); .

However, for high density resolutions or in the horizontal orientation, it'll always default to the 175, which isn't a factor of the width, and it'll have gaps in between each object in the GridView. 但是,对于高密度分辨率或水平方向,它将始终默认为175,这不是宽度的因素,并且在GridView中的每个对象之间都会有间隙。 How can I make it gapless? 我怎样才能做到完美无缺?

Hmmmm It's kind of obvious that in landscape it will be greater than 175 (any screen that has more than 700 pixels will do it), so the 175 will be applied. 嗯,很明显,在横向中它将大于175(任何具有700像素以上的屏幕都可以做到),因此将应用175。 In a more general note, you should never use fixed pixel sizes, create an entry in your dimens.xml and then read it int pixels = getResources().getDimensionPixelSize(R.dimen.whatever); 在更一般的注释中,永远不要使用固定的像素大小,不要在dimens.xml中创建一个条目,然后再读取它。int int pixels = getResources().getDimensionPixelSize(R.dimen.whatever); , but that will only adjust for density, not for horizontal space. ,但这只会调整密度,而不会调整水平空间。

Sounds like you should instead try to find the maximum number of icons you can fit at an approximate size and then use that size, instead of always trying to use 4. 听起来,您应该尝试以合适的尺寸找到可以容纳的最大图标数量,然后使用该尺寸,而不是总是尝试使用4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM