如何编写读取屏幕大小的代码。我有 4 种不同的布局,“layout”、“layout-land”、“layout-large”和“layout-large-land”
我需要为每个布局编写不同的代码,例如在“布局”中,我有这段代码 imagebutton1.setVisibility(View.VISIBLE);
,但是在横向屏幕上,我删除了 imagebutton1。所以我计划使用 if else 语句,但我不知道如何使用 android java 确定屏幕的大小,这里需要一些指导。
最佳答案
从技术上讲,您可以这样做:
ImageButton imageButton = findViewById(R.id.image_button);
if (imageButton1 != null) {
// if the imagebutton isn't found in the view hierarchy,
// then don't attempt to manipulate it.
imagebutton1.setVisibility(View.VISIBLE);
}
否则你可以使用:
Configuration conf = getResources().getConfiguration();
boolean isLarge = (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_LARGE) ==
Configuration.SCREENLAYOUT_SIZE_LARGE;
boolean isLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE);
boolean isLargeLand = isLarge && isLandscape;
关于android,用于横向和纵向的不同代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11381221/