如何编写读取屏幕大小的代码。我有 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;