【问题标题】:Android navigation bar size in xmlxml中的Android导航栏大小
【发布时间】:2014-10-25 13:22:50
【问题描述】:

有没有办法在类似这样的xml文件中获取android中导航栏的大小?

 android:paddingTop="?android:attr/actionBarSize"

actionBarSize 是导航栏的大小?

【问题讨论】:

  • 您的意思是“导航栏”中的“ActionBar”?如果需要获取ActionBar的高度,参考这个问题stackoverflow.com/questions/12301510/…
  • 不,我指的是导航栏(在没有硬件按钮的设备上显示的那个)。

标签: android android-layout android-xml


【解决方案1】:

在查看 android 源代码后,导航栏高度似乎有尺寸:

@android:dimen/navigation_bar_height

There 是链接到导航栏的其他维度,来自 android values/dimens.xml 的示例:

<!-- Height of the bottom navigation / system bar. -->
<dimen name="navigation_bar_height">48dp</dimen>
<!-- Height of the bottom navigation bar in portrait; often the same as @dimen/navigation_bar_height -->
<dimen name="navigation_bar_height_landscape">48dp</dimen>
<!-- Width of the navigation bar when it is placed vertically on the screen -->
<dimen name="navigation_bar_width">42dp</dimen>

【讨论】:

  • Error:(167, 39) Resource is not public. (at 'layout_marginBottom' with value '@android:dimen/navigation_bar_height').
  • @IonAalbers 您在哪个版本的 Android 上工作?
  • 同样的问题。有什么解决办法吗?
  • 有同样的问题。无法为非导航栏设备自定义布局。
  • 我遇到了和 Ion Aalbers 一样的问题。使用 Max 的解决方案对我有用,所以我将填充和边距移至 onCreate
【解决方案2】:

试试这个code:

Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
    return resources.getDimensionPixelSize(resourceId);
}
return 0;

【讨论】:

  • 在 XML 中特别请求的 OP。
【解决方案3】:

正如许多类似问题中所建议的那样,例如thisthisthisthis,仅仅获取导航栏高度可能还不够。我们需要考虑是否1。导航栏存在,2.是在底部,还是在右边,还是在左边,3.app是多窗口打开的。

有一个简单的单行解决方案

android:fitsSystemWindows="true"

或以编程方式

findViewById(R.id.your_root_view).setFitsSystemWindows(true);

您也可以通过

获得根视图
findViewById(android.R.id.content).getRootView();
or
getWindow().getDecorView().findViewById(android.R.id.content)

有关获取根视图的更多详细信息,请参阅 - https://stackoverflow.com/a/4488149/9640177

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多