简体   繁体   English

根据Android屏幕尺寸以编程方式设置布局宽度和高度

[英]Set layout width and height programmatically base on Screen size Android

I have a some problem with my layout when my screen change from portrait to landscape mode. 当我的屏幕从portrait模式更改为landscape模式时, layout存在一些问题。

issues I'm facing 我面临的问题

I have a SquareLayout widget which extends a LinearLayout , which help me to make a perfect square, and that work fine just as I want it to in portrait mode but if I change the from the orientation to landscape the square will be small, I want to be able to get the same size in Landscape mode and there I will have a scrollView to help with the height. 我有一个SquareLayout widget ,该widget扩展了LinearLayout ,可以帮助我制作一个完美的正方形,并且可以按我希望的方式在portrait模式下正常工作,但是如果我从orientation更改为landscape则正方形会很小,我想要为了能够在Landscape模式下获得相同的大小,在那里我将有一个scrollView来帮助调整高度。

1) How to get the size of the screen and base on that set the layout width and height programmatically. 1)如何获得屏幕尺寸,并以此为基础以编程方式设置layout widthheight

2) If I can get the size of the square in portrait mode and pass it to my landscape square, In that case I think the width will be fine and if i put a scrollView the user can be scroll for the full height. 2)如果我可以在portrait模式下获得正方形的大小并将其传递给我的landscape正方形,在那种情况下,我认为width会很好,如果我放置了scrollView则用户可以scroll到整个高度。

The reason I'm more concerned about this is because of android device screen size, That is why I want to know the size of the screen and base on that set the width and height. 我更关心这个的原因是因为android设备的屏幕尺寸,这就是为什么我想知道屏幕的尺寸并基于此设置宽度和高度的原因。

You can do as suggested above programmatically, or you could just create a new folder for your layouts called layout-land and then create the same layout you want just for landscape and then it will auto change to the new layout when the device is flipped. 您可以按照上述建议进行编程,也可以只为布局创建一个名为layout-land的新文件夹,然后创建与横向相同的布局,然后在翻转设备时它将自动更改为新布局。

More info can be found here: http://developer.android.com/training/basics/supporting-devices/screens.html 可以在这里找到更多信息: http : //developer.android.com/training/basics/supporting-devices/screens.html

From within activity: 在活动中:

int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();

From Non Activity Class in which you have context of activity 来自您具有活动上下文的非活动类

WindowManager windowManager = (WindowManager)mContext.getSystemService(WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
int height = windowManager.getDefaultDisplay().getHeight()

As suggested by w9jds : If you need the landscape layout to be different than the portrait layout, you need to make a layout-land folder in the res folder. w9jds所建议:如果需要横向布局与纵向布局不同,则需要在res文件夹中创建一个layout-land文件夹。 Now you can make two different xml files wrt orientation for the same screen. 现在,您可以为同一屏幕制作两个不同的xml文件。 Just Make sure that they both have the same names. 只要确保它们具有相同的名称即可。 Make the landscape one with the ScrollView and the portrait one without the ScrollView . 使景观与一个在ScrollView和人像一个没有ScrollView Calculate the width and height of the square in the same manner for both layouts. 对于两种布局,以相同的方式计算正方形的宽度和高度。

For more Information about multiple layouts in android, read this article. 有关多布局在android系统的更多信息,请阅读文章。

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

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