【问题标题】:Get Navigation bar height in xamarin forms?在 xamarin 表单中获取导航栏高度?
【发布时间】:2017-10-13 07:51:42
【问题描述】:

我正在做一个需要获取导航栏高度的项目。我正在使用以下方法获取整个屏幕高度:

Helpers.ApplicationContext.ScreenHeight = (int)Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density;
Helpers.ApplicationContext.ScreenWidth = (int)Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density;

但我无法获得导航栏的高度。

任何人都可以提出一个想法来获取我页面的导航栏高度。

提前致谢。

【问题讨论】:

    标签: xamarin.ios xamarin.forms xamarin.android navigationbar


    【解决方案1】:

    对于 iOS 这可能会对您有所帮助:

    var navbarHeight = YourViewControllerInstance.NavigationController?.NavigationBar.Frame.Height;
    

    对于安卓:

    TypedArray styledAttributes = this.Theme.ObtainStyledAttributes(
    new int[] { Android.Resource.Attribute.ActionBarSize });
    var actionbarHeight = (int) styledAttributes.GetDimension(0, 0);
    styledAttributes.recycle();
    

    上面的例子给出了像素高度。请在下面找到更精细的示例,这些示例提供了 Pixel 和 DP 的高度:

    TypedValue tv = new TypedValue();
    int actionBarHeightInPixel = 0;
    int actionBarHeightInDP = 0;
    DisplayMetrics metrics = Resources.DisplayMetrics;
    
    if (Theme.ResolveAttribute(Resource.Attribute.actionBarSize, tv, true))
    {
        actionBarHeightInPixel = TypedValue.ComplexToDimensionPixelSize(tv.Data, metrics);
        actionBarHeightInDP = actionBarHeightInPixel / (int)metrics.Density;
    }
    

    【讨论】:

    • 嗨米兰,我正在尝试您的代码在 android 和 ios 中获取导航栏高度,在 Ios 中它工作得很好,但在 android 中我获得了非常巨大的价值,这是错误的价值或我的其他价值.我在 android 中超过 200。
    • Deepak,我已经更新了我的答案。请试一试,如果能解决您的问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多