【问题标题】:XamarinForm : Android : Statusbar overlap contentpageXamarinForm:Android:状态栏重叠内容页
【发布时间】:2017-09-14 13:21:55
【问题描述】:

在基于 Xamarin Form 的 Android 应用程序中,我遇到了 Statusbar 与 ContentPage 重叠的问题。

在 XAML 中为 ContentPage 设置硬编码厚度值可能不适用于每个 Android 设备。

所以,我在博客上找到的解决方案很少,但对任何人都不起作用。到处都写着从 Xamarin.Droid 项目计算状态栏高度并将填充设置到内容页面。

所以请任何人建议如何通过计算状态栏高度将填充设置为运行时内容页面的顶部?

提前致谢。

【问题讨论】:

    标签: android xamarin xamarin.forms overlap android-statusbar


    【解决方案1】:

    我找到了解决办法:

    为此,我们需要创建自定义渲染器。

    请从 Xamarin.Droid 项目中找到下面提到的 PageRenderer 代码:

    [assembly: ExportRenderer(typeof(ContentPage), typeof(MyContentPageRenderer))]
    

    命名空间 ProjectName.Droid.Renderer

    {

    public class MyContentPageRenderer : PageRenderer
    {
        public MyContentPageRenderer()
        {
        }
    
    
        protected override void OnElementChanged(ElementChangedEventArgs<Page> e)
        {
            base.OnElementChanged(e);
    
    
        }
    
        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
    
    
    
            float pixWidth = (float)Resources.DisplayMetrics.WidthPixels;
            float fdpWidth = (float)App.Current.MainPage.Width;
            float pixPerDp = pixWidth / fdpWidth;
    
            this.Element.Padding = new Thickness(0, MainActivity.StatusBarHeight/pixPerDp, 0, 0);
    
        } } }
    

    请找到下面提到的“MainActivity”代码用于 StatusBarHeight 计算:

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public static int StatusBarHeight;
    
        protected override void OnCreate(Bundle bundle)
        {
    
            base.OnCreate(bundle);
    
            global::Xamarin.Forms.Forms.Init(this, bundle);
    
            if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                Window.DecorView.SystemUiVisibility = 0;
                var statusBarHeightInfo = typeof(FormsAppCompatActivity).GetField("_statusBarHeight", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                statusBarHeightInfo.SetValue(this, 0);
                Window.SetStatusBarColor(new Android.Graphics.Color(18, 52, 86, 255));
            }
    
            LoadApplication(new App());
    
            StatusBarHeight = getStatusBarHeight();
    
        }
    
        public int getStatusBarHeight()
        {
    
            int statusBarHeight = 0, totalHeight = 0, contentHeight = 0;
            int resourceId = Resources.GetIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0)
            {
                statusBarHeight = Resources.GetDimensionPixelSize(resourceId);
    
    
            }
            return statusBarHeight;
        }
    }
    

    这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 2015-01-21
      • 2013-09-24
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多