0

在 Android 文档:“Supporting Multiple Screens”中,Google 描述了如何为不同的屏幕尺寸采用不同的布局方案:

res/layout/my_layout.xml  
res/layout-small/my_layout.xml  
res/layout-large/my_layout.xml  
res/layout-large-land/my_layout.xml  
res/layout-xlarge/my_layout.xml  
...

问题是对于每个布局都达到了相同的代码:在'onCreate'中我正在使用:
setContentView(R.layout.my_layout); 当然会根据屏幕大小调用正确的布局(xml)。
我想要的是 xlarge 和 small 屏幕会有非常不同的布局,在这种情况下 Java 代码会非常不同。我的问题是如何在代码中区分这些情况?我必须使用一些 if/else 还是 android 可以自动执行?

4

2 回答 2

2

我的问题是如何在代码中区分这些情况?我必须使用一些 if/else 还是 android 可以自动执行?

您将不得不“使用一些 if/else”,如下所示:

if (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_LARGE)==Configuration.SCREENLAYOUT_SIZE_LARGE) {
  // yes, we are large
}
else {
  // no, we are not
}
于 2011-04-06T23:07:01.170 回答
0

我希望导航抽屉在较小的屏幕尺寸上启动时打开,并在平板电脑上启动时保持关闭。

这是我最终得到的代码:

if(getResources().getConfiguration().smallestScreenWidthDp < 600){
            //Open the navigation drawer automatically
            ((DrawerLayout)    findViewById(R.id.drawer_layout)).openDrawer(GravityCompat.START);
        }else{
            //Don't do anything
        }
于 2016-06-13T14:21:18.357 回答