【问题标题】:Ho to get s8 useable screen android?如何获得 s8 可用屏幕 android?
【发布时间】:2018-01-30 02:56:35
【问题描述】:

我是安卓的菜鸟。我关于 18:9 设备可用高度的问题。 当我尝试以这些宽高比获得可用屏幕时,我的应用程序在所有 android 设备上都可以正常工作,但是当我在三星 Galaxy s8 中编译时它无法正常工作。 我正在尝试获得可用的设备屏幕。 我已经尝试过这些链接中的方法

https://stackoverflow.com/questions/43628047/how-to-support-189-aspect-ratio-in-android-apps

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;
    }

当我尝试设置参数 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); ...

}

   public class StudentLoginView extends FrameLayout { ...
    FrameLayout.LayoutParams cp = new FrameLayout.LayoutParams(width, height);
    setLayoutParams(cp); ...

}

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

我的问题和这个链接很相似

android Navigation Bar hiding and persantage of usable screen overlap

【问题讨论】:

  • 什么叫可用高度?
  • 第二个链接粘贴错误。

标签: android uinavigationbar display aspect-ratio


【解决方案1】:

您可以使用装饰视图获得可用高度(即使在 Galaxy S8 上显示或不显示 NavBar):

    //Get the correct screen size even if the device has a hideable navigation bar (e.g. the Samsung Galaxy S8)
    View decorView = getWindow().getDecorView(); //if you use this in a fragment, use getActivity before getWindow()
    Rect r = new Rect();
    decorView.getWindowVisibleDisplayFrame(r);
    int screenHeight = r.bottom; // =2220 on S8 with hidden NavBar and =2076 with enabled NavBar
    int screenWidth = r.right; // =1080 on S8

【讨论】:

    【解决方案2】:

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

    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
            }
        });
    

    This answer was taken from here.

    【讨论】:

      猜你喜欢
      • 2018-02-22
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2011-08-25
      • 1970-01-01
      相关资源
      最近更新 更多