何获得s8可用屏幕的android?

Kar*_*opu 6 android aspect-ratio uinavigationbar display

我是android的菜鸟。我关于18:9设备可用高度的问题。当我尝试在这些纵横比中获得可用的屏幕时,我的应用程序可以正常运行所有android设备,但是何时?在三星银河s8中编译它不起作用。我正在尝试获取可用的设备屏幕。我已经尝试过这些链接中的方法

/sf/ask/3053963321/

https://android-developers.googleblog.com/2017/03/update-your-app-to-take-advantage-of.html

我动态地使用

DisplayMetrics metrics = this.getResources().getDisplayMetrics(); width = metrics.widthPixels; height = metrics.heightPixels ;

我试过了

private int getSoftButtonsBarHeight() {
        // getRealMetrics is only available with API 17 and +
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int usableHeight = metrics.heightPixels;
            getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
            int realHeight = metrics.heightPixels;
            if (realHeight > usableHeight)
                return realHeight - usableHeight;
            else
                return 0;
        }
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

当我尝试设置参数MATCH_PARENT的高度时,它工作得很好。但是我需要找到可用的高度像素来按比例设计其他视图。

实际上,这些代码DisplayMetrics metrics = this.getResources().getDisplayMetrics(); height = metrics.heightPixels ;在我的Activity中有效,但是当我尝试在另一个窗口(从FramaLayout扩展并添加到Activity中)中使用它时,则无法正常工作。

这是我的代码块

public class StudentLoginActivity extends Activity {  ...
    FrameLayout.LayoutParams containerParams = new ScrollView.LayoutParams(width, height-sb);
    container = new FrameLayout(this);
    container.setLayoutParams(containerParams);
    container.setBackgroundColor(Color.rgb(248,248,248));
    loginStudentView = new StudentLoginView(this);
    container.addView(loginStudentView); ...
Run Code Online (Sandbox Code Playgroud)

}

   public class StudentLoginView extends FrameLayout { ...
    FrameLayout.LayoutParams cp = new FrameLayout.LayoutParams(width, height);
    setLayoutParams(cp); ...
Run Code Online (Sandbox Code Playgroud)

}

但是这个问题与android navigationBar的高度有关,因为当我显示导航栏时没有问题,但是如果我隐藏了NavigationBar,它仍然无法调整大小,但是应用程序仍在工作,屏幕上有一个导航栏(但是我隐藏了navigationBar)。

我的问题很相似

android导航栏隐藏和可用屏幕重叠

swe*_*rly 0

如果您有一个设置为与父级宽度和高度(整个屏幕)匹配的视图,则可以将侦听器附加到该视图,然后获取其宽度和高度。

View myView = findViewById(R.id.my_view);
myView.addOnLayoutChangeListener(new OnLayoutChangeListener() {

        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight,
                int oldBottom) {
            // its possible that the layout is not complete in which case
            // we will get all zero values for the positions, so ignore the event
            if (left == 0 && top == 0 && right == 0 && bottom == 0) {
                return;
            }

           // Do what you need to do with the height/width since they are now set
        }
    });
Run Code Online (Sandbox Code Playgroud)

这个答案是从这里获取的。