如何在Xamarin Android中获取导航栏高度?

时间:2018-11-15 19:18:53

标签: android xamarin android-activity xamarin.android navigationbar

如何在Xamarin Android中获取顶部导航栏的高度?下面是Java代码

public static int getSceenHeight(Activity activity) {
   //obsolete now
    return activity.getWindowManager().getDefaultDisplay().getHeight()+getNavigationBarHeight(activity);
}

 public static int getNavigationBarHeight(Activity activity) {
    if (!isNavigationBarShow(activity)){
        return 0;
    }
    Resources resources = activity.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height",
            "dimen", "android");
    int height = resources.getDimensionPixelSize(resourceId);
    return height;
}

public static boolean isNavigationBarShow(Activity activity){

        Display display = activity.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        Point realSize = new Point();
        display.getSize(size);
        display.getRealSize(realSize);
        return realSize.y!=size.y;

    }
}

如何在Xamarin Android中应用它?

3 个答案:

答案 0 :(得分:1)

对于Xamarin / Xamarin.Form和Android,似乎一直对所谓的NavigationBar感到困惑。我真的希望Xamarin能够解决这个问题,使其与android保持一致。

反正打来电话...

int resourceId = resources.GetIdentifier("navigation_bar_height",
            "dimen", "android"); 

...将返回底部NavigationBar(Android)(操作系统的一部分按钮)的值。从上面的评论看来,您正在寻找Xamarin NavigationBar(顶部的菜单,其中包含菜单Title等(Bellow状态栏))。

请尝试以下

var styledAttributes = Context.Theme.ObtainStyledAttributes(new[] { Android.Resource.Attribute.ActionBarSize });
var actionBarSize = (int)styledAttributes.GetDimension(0, 0);
styledAttributes.Recycle();

感谢Fred的帖子here

答案 1 :(得分:0)

我不知道您将如何使用它,因为您说过我已经将您上面的方法转换为C#

public static int getSceenHeight(Activity activity)
{
    //obsolete now
    return activity.WindowManager.DefaultDisplay.Height + getNavigationBarHeight(activity);
}

public static int getNavigationBarHeight(Activity activity)
{
    if (!isNavigationBarShow(activity))
    {
        return 0;
    }
    Resources resources = activity.Resources;
    int resourceId = resources.GetIdentifier("navigation_bar_height",
            "dimen", "android");
    int height = resources.GetDimensionPixelSize(resourceId);
    return height;
}

public static bool isNavigationBarShow(Activity activity)
{
    if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBeanMr1)
    {
        Display display = activity.WindowManager.DefaultDisplay;
        Point size = new Point();
        Point realSize = new Point();
        display.GetSize(size);
        display.GetRealSize(realSize);
        return realSize.Y != size.Y;
    }
    else
    {
        bool menu = ViewConfiguration.Get(activity).HasPermanentMenuKey;
        bool back = KeyCharacterMap.DeviceHasKey(Keycode.Back);
        if (menu || back)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

答案 2 :(得分:0)

获取状态栏的方法(如您所说的那样显示信号,电池等的顶部)

private int getStatusBarHeight()
{
    Resources res = this.Resources;
    int resourceId = res.GetIdentifier("status_bar_height", "dimen", "android");
    int height = res.GetDimensionPixelSize(resourceId);
    return height;
}

The method to get navigation bar
        private int getNavigationBarHeight()
{
            Resources res = this.Resources;
            int resourceId = res.GetIdentifier("navigation_bar_height", "dimen", "android");
            int height = res.GetDimensionPixelSize(resourceId);
            return height;
}