Android 根据屏幕尺寸更改图像尺寸?
所以我需要根据屏幕的区域更改图像的大小。图像必须是屏幕高度的一半,否则它会重叠一些文本。
所以高度= 1/2 屏幕高度。 宽度=高度*纵横比(只是想保持纵横比相同)
我发现了一些东西:
Display myDisplay = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width =myDisplay.getWidth();
int height=myDisplay.getHeight();
但是我如何改变java中的图像高度?甚至 XML(如果可能的话)?我似乎找不到有效的答案。
So I need to change the size of an image depending on the area of the screen. The image will have to be half of the screen height, because otherwise it overlaps some text.
So Height= 1/2 Screen Height.
Width = Height*Aspect Ratio (Just trying to keep the aspect ratio the same)
I found something that was:
Display myDisplay = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width =myDisplay.getWidth();
int height=myDisplay.getHeight();
But how would I change image height in java? or even XML if possible? I can't seem to find a working answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在代码中使用
LayoutParams
来完成此操作。不幸的是,没有办法通过 XML 指定百分比(不是直接的,你可以搞乱权重,但这并不总是有帮助,而且它不会保持你的纵横比),但这应该对你有用:可能过于复杂,也许有更简单的方法?不过,这是我首先尝试的。
You can do this with
LayoutParams
in code. Unfortunately there's no way to specify percentages through XML (not directly, you can mess around with weights, but that's not always going to help, and it won't keep your aspect ratio), but this should work for you:Might be overcomplicated, maybe there's an easier way? This is what I'd first try, though.