我正在检查导航栏的可见度。在三星Galaxy S8上,我可以切换导航条的可见度。
我尝试了很多不同的方法来检查可见度,但没有一个在银河S8上工作。
一些示例:(无论显示还是隐藏,它们总是返回相同的值)
ViewConfiguration.get(getBaseContext()).hasPermanentMenuKey()总是返回false KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)总是返回false KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME)总是返回true
即使计算出导航栏的高度(How do I get the height and width of the Android Navigation Bar programmatically?),它也不能工作。
发布于 2018-05-31 14:54:25
也许这对某人有用。用户可以隐藏导航条,因此检测可见性的最佳方法是订阅此事件。它起作用了。
object NavigationBarUtils {
// Location of navigation bar
const val LOCATION_BOTTOM = 0
const val LOCATION_RIGHT = 1
const val LOCATION_LEFT = 2
const val LOCATION_NONE = 3
fun addLocationListener(activity: Activity, listener: (location: Int) -> Unit) {
ViewCompat.setOnApplyWindowInsetsListener(activity.window.decorView) { view, insets ->
val location = when {
insets.systemWindowInsetBottom != 0 -> LOCATION_BOTTOM
insets.systemWindowInsetRight != 0 -> LOCATION_RIGHT
insets.systemWindowInsetLeft != 0 -> LOCATION_LEFT
else -> LOCATION_NONE
}
listener(location)
ViewCompat.onApplyWindowInsets(view, insets)
}
}
}发布于 2019-05-13 02:33:39
由于在编写本报告时唯一的答案是Kotlin,这里有一个Java替代方案:
import android.content.res.Resources;
import android.support.v4.view.OnApplyWindowInsetsListener;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.WindowInsetsCompat;
import android.view.View;
public class NavigationBar {
final int BOTTOM = 0;
final int RIGHT = 1;
final int LEFT = 2;
final int NONE = 3;
private int LOCATION = NONE;
private View view;
NavigationBar(View view) {
this.view = view;
}
int getNavBarLocation() {
ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
if (insets.getSystemWindowInsetBottom() != 0)
LOCATION = BOTTOM;
else if (insets.getSystemWindowInsetRight() != 0)
LOCATION = RIGHT;
else if (insets.getSystemWindowInsetLeft() != 0)
LOCATION = LEFT;
else
LOCATION = NONE;
return insets;
}
});
return LOCATION;
}
int getNavBarHeight() {
Resources resources = view.getResources();
int resourceId = resources.getIdentifier(
"navigation_bar_height", "dimen", "android");
if (resourceId > 0)
return resources.getDimensionPixelSize(resourceId);
return 0;
}
}我还包括了如何获得高度,因为这往往将是下一步。
在您的活动中,您将使用基于视图的引用调用这些方法:
NavigationBar navigationBar = new NavigationBar(snackbarLayout);
if (navigationBar.getNavBarLocation() == navigationBar.BOTTOM) {
FrameLayout.LayoutParams parentParams =
(FrameLayout.LayoutParams) snackbarLayout.getLayoutParams();
parentParams.setMargins(0, 0, 0, 0 - navigationBar.getNavBarHeight());
snackbarLayout.setLayoutParams(parentParams);
}(此示例基于SnackBar边距不一致的常见问题)
https://stackoverflow.com/questions/46264447
复制相似问题