android,用于横向和纵向的不同代码

标签 android layout

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

相关文章:

java - 具有多个子网格的 ModelRenderable 仅显示一个子网格

java - GreenDao 和实体继承

java - 保存到 .doc 文件表

javascript - JQuery,隐藏在 Firefox 中不起作用

不同分辨率缩放布局的Android调整大小按钮

Android Scrollview 不显示所有内容

Android 注册 GTalk 传入聊天

java - 代码中最好的 "identifying markers"有助于快速导航到代码中的某个位置?

html - 为什么这个 html/css/rails 模板没有正确嵌套?

android - 以编程方式更改的可见性与权重不匹配