Ho 获得 s8 可用屏幕安卓?



我是安卓的菜鸟。我关于 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 ;在我的活动中工作,但是当我尝试在另一个窗口中使用它时,我从FramaLayout扩展并添加到活动中,它不起作用。

这是我的代码块

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导航栏高度有关,因为当我显示导航栏时没有问题,但是如果我隐藏导航栏,则不会调整应用程序的大小仍然在屏幕上有一个导航栏(但我隐藏了导航栏(。

我的问题与此链接非常相似

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

您可以使用装饰视图获得可用高度(即使在带有或不显示导航栏的Galaxy S8上(:

//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

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

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

这个答案是从这里拿来的。

最新更新