设置UI在不同设备上相同
我正在为 Android 创建游戏,但我不知道如何在不同设备上保持相同的布局。我希望它从我的破烂小手机到 Galaxy S 看起来都一样。这就是我现在正在做的事情。我尝试在构造函数中获取尺寸,但它给了我错误,所以我在 onDraw 类中构建了自己的迷你构造函数。
protected void onDraw(Canvas canvas) {
if (startup){//constructor part 2
startup=false;
height=canvas.getHeight();
width=canvas.getWidth();
xinc=width/(7/2);
yinc=height/5;
radius=width/6;
等等。这是最好的方法吗?在我看来,应该有另一种更好的方法来做到这一点。另外,所有 Android 设备都具有相同的(我认为它称为长宽比)宽度/高度吗?提前致谢。
-德里克
I am creating a game for android and I cant figure out how to keep the layout the same for different devices. I want it to look the same from my crummy little phone to a galaxy s. Here is what I am doing now. I tried to get the dimensions in the constructor, but it gave me errors for that, so i built my own mini constructor in the onDraw class.
protected void onDraw(Canvas canvas) {
if (startup){//constructor part 2
startup=false;
height=canvas.getHeight();
width=canvas.getWidth();
xinc=width/(7/2);
yinc=height/5;
radius=width/6;
and so on. Is this the best way to do it? It seems to me that there should be another better way to do this. Also, do all android devices have same (I think its called the aspect ratio) width/height? Thanks in advance.
-Derek
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Android 中,UI 通常使用 xml 编写并保存在布局文件夹中。不同的资源文件夹可以被赋予不同的名称,这将允许android根据不同的情况引用它们。
这个链接来自android文档,它解释了不同的名称资源文件夹可以占用。
假设您有一张图像在不同的手机中应具有不同的尺寸。
您可以将具有相同名称的图像放在
drawable-ldpi/
drawable-mdpi/
drawable-hdpi/
中,因此对于imageview,当您使用该命令时
Android将检查手机的密度并自动从相应的文件夹中获取图标图像。可以对布局和“res”的其他子文件夹执行相同的方法
从您的问题中我看到您正在重写视图的“onDraw”方法来执行 UI。如果是这样的话,据我所知,您将必须测量可用空间并以编程方式创建和测量您的 UI。
In android UI are normally written using xml and kept in the layout folder. The different resource folders can be given different name which will allow android to reference them according to different conditions.
This link is from the android documentation and it explains the different names the resoucrce folders can take.
Suppose you have an image that should have different size in different phones.
You can put the image with the same name in
drawable-ldpi/
drawable-mdpi/
drawable-hdpi/
So for an imageview when you use the command
Android will check the density of the phone and take the icon image from the appropriate folder automatically. The same method can be done for layouts and the other subfolders of 'res'
From your question I see that you are overriding an 'onDraw' method of a view to do the UI. If thats the case, AFAIK you will have to measure the available space and create and measure your UI programatically.