使用 XML 的特定于平板电脑的 ImageView 宽度/高度/等值?
我正在使我的应用程序与平板电脑兼容,并且我正在尝试了解执行此操作的最佳方法是什么。
我有一个 GridView,每个图像下都有图像和文本。目前,每个图像的高度设置为 120dp 而不是 wrap_content,以便它能够正确缩放并对齐到其高度,并且一切看起来都均匀。方向变化效果很好,GridView 自动从 2xN 变为 3xN。
现在我想把它放在平板电脑上,根据方向(在 10 英寸平板电脑上),GridView 最终会变成 5xN 和 7xN。但是,为了使 GridView 看起来更好,我宁愿将图像高度更改为更大的值(例如 300dp)并且拥有较少的较大尺寸的项目,那么最好的方法是什么?
我不想只取出该元素并复制其所有值,理想情况下只更改一个。我想简单地将高度值放入 strings.xml 和 strings-xlarge.xml 中,然后从布局中引用它,我真的很讨厌重复代码/资源,
谢谢。
I'm making my app compatible with tablets, and I am trying to understand what the best way of doing this would be.
I have a GridView with images and text under each image. Currently the height of each image is set to 120dp rather than wrap_content so that it gets scaled and aligned properly to its height and everything looks even. The orientation change works great, and the GridView changes from 2xN to 3xN automatically.
Now that I want to put it on a tablet, the GridView ends up being 5xN and 7xN depending on orientation (on a 10" tablet). However, to make the GridView look better, I would rather change the image height to a larger value (say 300dp) and have fewer items that are larger in size. What's the best way of doing this?
I don't want to pull out just the element and replicate all its values, changing just one. Ideally, I'd like to simply be able to put the height value into something like strings.xml and strings-xlarge.xml, and then reference it from the layout. Is that possible? I really hate duplicating code/resources.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的是 dimen 标签。它的工作方式与字符串标签类似,但适用于尺寸。您可以在values/dimens.xml 中添加类似的内容:
在values-xlarge/dimens.xml 中:
您的XML 可以通过@dimen/image_size 引用尺寸,一切都会为您处理好。您还可以使用 Context 中的 getResources().getDimension(R.dimen.image_size) 以编程方式访问它。
请注意,从 3.2 开始,您可以在资源名称中使用像素要求(例如 w720dp),而不是像 xlarge 那样的“存储桶”。更多信息此处。
What you're looking for is the dimen tag. It works similarly to the string tag but for dimensions. You can have something like this in values/dimens.xml:
And in values-xlarge/dimens.xml:
Your XML can reference the size by @dimen/image_size and everything will be taken care of for you. You can also programatically access it with getResources().getDimension(R.dimen.image_size) from Context.
Note that as of 3.2, you can use pixel requirements (such as w720dp) in your resource names instead of "buckets" like xlarge. More info here.