在Xamarin.Forms android中使用正确的屏幕高度和宽度的全局解决方案?

时间:2018-03-19 14:48:36

标签: android xaml xamarin xamarin.forms

我正在为Android和iOS平台的Xamarin.Forms为客户开发一个新的应用程序,一切顺利,因为我决定在Android平台中查看不同屏幕尺寸的应用程序。

似乎是Xamarin.Forms忽略了系统导航托盘,每个视图中的每个元素都显示在系统栏下面。我知道这可以通过XAML.CS中的OnPlatform stament或SizeChanged事件来修复,但我想知道是否存在修复该问题的全局方法,而不是对每个页面进行少量修复。

看看我的一个页面。

我的应用程序中几乎所有页面都使用Grid完成,所以这里有3行,第二行有2列;在iOS页面中工作得很好...但是安卓......它真的有点痛苦。

提前致谢。

**来自主要活动的新增代码**

[Activity(Label = "MyBankingApp", Icon = "@drawable/icon", Theme = 
"@style/MainTheme", ConfigurationChanges = ConfigChanges.ScreenSize | 
ConfigChanges.Orientation, WindowSoftInputMode = SoftInput.AdjustResize,
          ScreenOrientation = ScreenOrientation.Portrait)]

我有这三种方法:

    public void AndroidBug5497WorkaroundForXamarinAndroid () {

        FrameLayout content = (FrameLayout) this.FindViewById (Android.Resource.Id.Content);
        mChildOfContent = content.GetChildAt (0);
        ViewTreeObserver vto = mChildOfContent.ViewTreeObserver;
        vto.GlobalLayout += (object sender, EventArgs e) => {
            possiblyResizeChildOfContent ();
        };
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.LayoutParameters;
    }

    private void possiblyResizeChildOfContent () {
        int usableHeightNow = computeUsableHeight ();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;

            frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;

            mChildOfContent.RequestLayout ();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight () {
        Android.Graphics.Rect r = new Android.Graphics.Rect ();
        mChildOfContent.GetWindowVisibleDisplayFrame (r);
        if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop) {
            return (r.Bottom - r.Top);
        }

        maxHeight = Math.Max(maxHeight, r.Bottom);
        return r.Bottom.Equals(maxHeight) ? maxHeight : (int)(maxHeight* 0.8);
    }

请注意,我主要是iOS开发人员,因此有时候我很难理解Android。

my styles.xml

<resources>
  <style name="MainTheme" parent="MainTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">false</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#181F27</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#181F27</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#7FAD30</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="windowActionModeOverlay">true</item>
    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>
  <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#7FAD30</item>
  </style>
     <style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splashbg</item>
    <item name="android:windowNoTitle">true</item>  
    <item name="android:windowFullscreen">true</item>  
    <item name="android:windowContentOverlay">@null</item>  
    <item name="android:windowActionBar">true</item>  
  </style>
</resources>

Android system navigation bar overlay in Xamarin.Forms

1 个答案:

答案 0 :(得分:0)

  • 在您的Android项目中添加以下主题:

<强>值/ styles.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
</style>

<style name="DrawerArrowStyle" 
parent="@style/Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
 <item name="color">#FFFFFF</item>
</style>

<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#003399</item>
<item name="colorPrimaryDark">#003399</item>
<item name="colorControlHighlight">#003399</item>
<item name="colorAccent">#012348</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

</resources>
  • 创建名为values-v21的文件夹并添加名为styles.xml的XML并向其添加以下代码:

     <?xml version="1.0" encoding="utf-8" ?>
     <resources>
     <style name="MyTheme" parent="MyTheme.Base">
     <item name="android:windowContentTransitions">true</item>
     <item name="android:windowAllowEnterTransitionOverlap">true</item>
     <item name="android:textAllCaps">false</item>
     <item name="android:windowAllowReturnTransitionOverlap">true</item>
     <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
     <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
     </style>
     </resources>
    

在所有Android活动中使用名称myTheme作为应用主题:

这样的事情:

[Activity(Label = "MyBankingApp", Icon = "@drawable/icon", Theme = "@style/MyTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, WindowSoftInputMode = SoftInput.AdjustResize, ScreenOrientation = ScreenOrientation.Portrait)]

如果它不起作用,则还原