栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Android中获取屏幕高度

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Android中获取屏幕高度

工作解决方案:

创建两个虚拟视图:

 <View    android:id="@+id/top"    android:layout_height="0dp"    android:layout_width="match_parent"    android:layout_alignParentTop="true" /><View        android:id="@+id/bottom"        android:layout_height="0dp"        android:layout_width="match_parent"        android:layout_alignParentBottom="true" />

现在,您应该调用

getLocationOnScreen(int[])
这两个视图以接收它们的位置。此函数返回2个整数的数组。第一个是x,第二个是y。我们只需要y。还要注意,只有在创建所有视图并
onCreate
退出方法之后,才返回正确的值。

我在从viewpager调用的常规片段上测试了此代码。

代码在

onCreateView
我的片段方法中。我仍然必须使用a
postDelayed
来获取正确的值。

final View top = (View) view.findViewById(R.id.top);    final View bottom = (View) view.findViewById(R.id.bottom);    new Handler().postDelayed(new Runnable() {        @Override        public void run() { int topLoc[] = new int[2]; top.getLocationOnScreen(topLoc); int BottomLoc[] = new int[2]; bottom.getLocationOnScreen(BottomLoc); Log.d(Constants.debug, "topY: "+ topLoc[1]+" BottomY:" + BottomLoc[1]);        }    },4000);

我有一个Nexus 4。

在常规模式下(状态栏和显示的软按钮):

07-05 02:28:23.367      565-565/com.torcellite.xx D/xx: topY: 50 BottomY:1182

在全屏模式下(不显示状态栏或软按钮):

07-05 02:29:17.360      707-707/com.torcellite.xx D/xx: topY: 0 BottomY:1280

您现在要做的就是减去。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/429098.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号