Android:横屏模式添加了边缘,bound.left或MainActivity的X值被设置
创始人
2024-10-13 12:31:03
0

这个问题通常出现在使用 SurfaceView 或自定义视图时。在横屏模式下,在屏幕左侧和右侧添加了黑色条纹,这是由于屏幕尺寸和视图尺寸不匹配所造成的。为了解决这个问题,可以在代码中根据屏幕宽度动态计算视图宽度,并调整视图的位置来避免条纹出现。示例代码如下:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        View view = findViewById(R.id.your_view_id);
        int viewWidth = screenWidth;
        int viewHeight = screenHeight - getStatusBarHeight() - getNavigationBarHeight();
        // 如果需要设置视图宽高比例,可以在这里计算
        view.setLayoutParams(new FrameLayout.LayoutParams(viewWidth, viewHeight));
        view.setX(-getStatusBarHeight()); // 如果有状态栏,需要加上状态栏高度
    }
}

private int getStatusBarHeight() {
    int height = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        height = getResources().getDimensionPixelSize(resourceId);
    }
    return height;
}

private int getNavigationBarHeight() {
    int height = 0;
    int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        height = getResources().getDimensionPixelSize(resourceId);
    }
    return height;
}

其中,getStatusBarHeight()getNavigationBarHeight() 方法用于获取状态栏和导航栏高度,以便在计算视图高度时排除它们。最后在 onWindowFocusChanged() 方法中计算视图宽高并设置位置,注意要在 super.onWindowFocusChanged(hasFocus); 之后调用。

相关内容

热门资讯

wepokerplus开挂!大... wepokerplus开挂!大菠萝789辅助器下载(透视)挂-都是分享有挂运大菠萝789辅助器下载辅...
透视专业!德州透视hhpoke... 透视专业!德州透视hhpoker(透视)hhpoker视频巡查真的假的,教程教程书(真的有挂)-哔哩...
pokemmo脚本辅助器下载!... pokemmo脚本辅助器下载!wepoker辅助器最新版本更新内容(透视)神器-切实有挂是有挂1、w...
透视辅助!wepoker私人局... 透视辅助!wepoker私人局辅助挂(透视)aapoker插件,教程练习(有挂规律)-哔哩哔哩在进入...
如何判断wpk辅助软件的真假!... 如何判断wpk辅助软件的真假!哈糖大菠萝破解器(透视)方法-都是揭幕真的有挂1、许多玩家不知道如何判...
透视总结!德扑HHpoker有... 透视总结!德扑HHpoker有挂吗(透视)hhpoker透视脚本下载,教程手筋(果真有挂)-哔哩哔哩...
aapoker万能辅助器!we... aapoker万能辅助器!wepoker怎么开辅助(透视)app-一贯了解有挂一、aapoker万能...
透视了解!cloudpoker... 透视了解!cloudpoker开挂(透视)wepoker底牌透视,教程练习(果真有挂)-哔哩哔哩1、...
wepoker软件安装包!hh... wepoker软件安装包!hhpoker德州机器人(透视)技巧-真是总结是真的挂1)hhpoker德...
透视开挂!hhpoker是真的... 透视开挂!hhpoker是真的假的(透视)安装不了wepoker,教程攻略(有挂技巧)-哔哩哔哩1、...