Android 如何获取Activity的窗口宽度和高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12428838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get an Activity's window width and height?
提问by oracleicom
I have a ListFragment that adds 2 views in the onActivityCreated() method - a MapView generic View (persistent header). I'd like the ListView to display underneath the MapView and header in portrait mode and on the right of the MapView and underneath the header in Landscape mode. I'd also like the MapView's height (in portrait) and width (in landscape) to be a percentage of the available window size (minus action bar, status bar and navigation bar - if one exists on the device). To accomplish this I believe I would need to get the window size and calculate the appropriate heights, widths and layout margins for all 3 views. I've tried a few things recommended in similar questions but the solution always returns the height/width of the device and not the window size of the Activity.
我有一个 ListFragment,它在 onActivityCreated() 方法中添加了 2 个视图 - MapView 通用视图(持久标题)。我希望 ListView 在纵向模式下显示在 MapView 和标题下方,在 MapView 右侧以及横向模式下显示在标题下方。我还希望 MapView 的高度(纵向)和宽度(横向)是可用窗口大小(减去操作栏、状态栏和导航栏 - 如果设备上存在)的百分比。为了实现这一点,我相信我需要获取窗口大小并计算所有 3 个视图的适当高度、宽度和布局边距。我已经尝试了类似问题中推荐的一些东西,但解决方案总是返回设备的高度/宽度,而不是活动的窗口大小。
Any ideas. Thanks in advance.
有任何想法吗。提前致谢。
采纳答案by oracleicom
I was able to solve this by using a ViewTreeObserver.
我能够通过使用 ViewTreeObserver 来解决这个问题。
ViewTreeObserver vtObserver = view.getViewTreeObserver();
vtObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// check if the view's height and/or width > 0
// don't forget to remove the listener when done using removeOnGlobalLayoutListener
}
});
回答by Ajmal Salim
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
回答by chris-tulip
For the scaling you can achieve by doing the following:
对于缩放,您可以通过执行以下操作来实现:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
LayoutParams params = new LayoutParams(metrics.widthPixels * desiredWidthPercentage, metrics.heightPixels * desiredHeightPercentage);
and then apply the params to the view you want.
然后将参数应用于您想要的视图。
For the different orientations I'd suggest using different layouts in the layout and layout-land folders or else dynamically adding rules as follows:
对于不同的方向,我建议在 layout 和 layout-land 文件夹中使用不同的布局,或者动态添加如下规则:
params.addRule(RelativeLayout.Above, ViewId);
to see all key words for rules see: http://developer.android.com/reference/android/widget/RelativeLayout.html
要查看规则的所有关键字,请参阅:http: //developer.android.com/reference/android/widget/RelativeLayout.html
回答by Stericson
I'm not sure the answer here is to get the screen dimensions but rather to use appropriate layouts for portrait and landscape that use the weight property to allocate the screen space between the different views.
我不确定这里的答案是获取屏幕尺寸,而是使用适当的纵向和横向布局,使用权重属性在不同视图之间分配屏幕空间。
I believe you can have a container layout that can host the other fragment layouts.
我相信您可以拥有一个可以托管其他片段布局的容器布局。
You can see some simple examples of using fragments inside of a layout xml file.
您可以看到一些在布局 xml 文件中使用片段的简单示例。
http://android-er.blogspot.com/2011/12/simple-exercise-of-fragment-inflate.html
http://android-er.blogspot.com/2011/12/simple-exercise-of-fragment-inflate.html
and here
和这里
http://portabledroid.wordpress.com/2011/04/19/programmatic-and-layout-fragments/
http://portabledroid.wordpress.com/2011/04/19/programmatic-and-layout-fragments/
Also, see this link for tips on providing layouts for different screen sizes: http://developer.android.com/guide/topics/resources/providing-resources.html
此外,有关为不同屏幕尺寸提供布局的提示,请参阅此链接:http: //developer.android.com/guide/topics/resources/providing-resources.html
I hope this points you in the right direction.
我希望这为您指明了正确的方向。
回答by Richie681
Like others have suggested, I'd use different layouts per orientation, and just handle it differently:
就像其他人建议的那样,我会在每个方向使用不同的布局,并以不同的方式处理它:
if(getResources().getBoolean(R.bool.is_land)){
//Set up mapview on the side
} else{
//Add header to listfragment
}
Using a boolean in res/values-land
在 res/values-land 中使用布尔值