我是机器人的菜鸟。我的问题是18:9设备的可用高度。当我试图在这些高宽比中获得可使用的屏幕时,我的应用程序可以很好地运行所有安卓设备,但是当ı在三星Galaxyı中编译时,它就不能工作了。我正在尝试获得可用的屏幕设备。我已经尝试过这些链接中的方法
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 navigationBar的高度有关,因为当我显示导航栏时没有问题,但是如果我隐藏navigationBar,则不会调整应用程序的大小,因为屏幕上有导航条(但我隐藏了navigationBar)。
我的问题和这个链接很相似
android Navigation Bar hiding and persantage of usable screen overlap
发布于 2018-06-19 09:57:51
您可以通过装饰视图获得可用的高度(即使在带或不显示NavBar的Galaxy 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发布于 2017-08-22 01:06:29
如果您的视图设置为匹配父视图的宽度和高度(整个屏幕),则可以将侦听器附加到该视图,然后获取其宽度和高度。
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
}
});https://stackoverflow.com/questions/45807174
复制相似问题