返回
获取底部导航栏的高度!实现页面布局的小细节!
Android
2024-01-22 01:14:09
相关类解析
public class NavigationBarHeight {
private static NavigationBarHeight instance;
public static NavigationBarHeight getInstance() {
if (instance == null) {
instance = new NavigationBarHeight();
}
return instance;
}
/**
* 获取底部导航栏高度
* @param activity
* @return
*/
@SuppressLint("PrivateApi")
public int getNavigationBarHeight(Activity activity) {
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
}
public class EasyFloat extends FrameLayout {
private static final String TAG = EasyFloat.class.getSimpleName();
private float x;
private float y;
private float startX;
private float startY;
private View contentView;
private int statusBarHeight;
private int navigationBarHeight;
private RectF currentFloatRect;
private WindowManager windowManager;
private WindowManager.LayoutParams layoutParams;
private boolean isShowing = false;
private int contentViewWidth;
private int contentViewHeight;
private Rect visibleFrame = new Rect();
private List<FloatWindowPermissionListener> listeners;
public EasyFloat(Context context) {
super(context);
init(context);
}
public EasyFloat(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public EasyFloat(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
statusBarHeight = NavigationBarHeight.getInstance().getStatusBarHeight(context);
navigationBarHeight = NavigationBarHeight.getInstance().getNavigationBarHeight(context);
windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
layoutParams.format = PixelFormat.RGBA_8888;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
layoutParams.gravity = Gravity.START | Gravity.TOP;
layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
// 如果非 Notch 屏手机,需要在show前预先设置y轴位置
if (!isSpecialNotchScreen()) {
layoutParams.y = navigationBarHeight;
}
}
补充内容:
1. 注意事项
- 在使用这些类时,需要确保您已正确配置 AndroidManifest.xml 文件,以允许应用程序使用必要的权限。
- 如果您正在使用带有底部导航栏的设备,则需要确保在布局中为其留出足够的空间。
- 这些类仅适用于 Android 设备,在其他平台上使用可能会导致错误或崩溃。
2. 代码示例
以下是一个使用这些类获取底部导航栏高度的示例:
int navigationBarHeight = NavigationBarHeight.getInstance().getNavigationBarHeight(this);
// 使用导航栏高度来调整布局
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(0, 0, 0, navigationBarHeight);
contentView.setLayoutParams(layoutParams);
3. 常见问题
-
我无法在模拟器中获取底部导航栏的高度。
这是因为模拟器通常没有物理底部导航栏。要获取底部导航栏的高度,您需要在真实设备上运行您的应用程序。
-
我正在使用带有底部导航栏的设备,但应用程序的内容被底部导航栏覆盖了。
您需要确保在布局中为底部导航栏留出足够的空间。您可以通过在布局的底部添加一个空白空间来实现这一点。
4. 结论
获取底部导航栏的高度对于在具有底部导航栏的设备上开发 Android 应用程序非常重要。使用本文中提供的类,您可以轻松地获取底部导航栏的高度,并将其用于调整应用程序的布局。