在Xamarin.Forms android中使用正确的屏幕高度和宽度的全局解决方案? [英] Global solution to use the correct screen height and width in Xamarin.Forms android?
问题描述
我正在使用Xamarin.Forms开发适用于Android和iOS平台的客户端的新应用程序,一切顺利,因为我决定在android平台中以不同屏幕尺寸查看该应用程序.
I'm developing a new app for a client with Xamarin.Forms for Android and iOS platforms, everything goes well since I decided review the app in different screen sizes in android platform.
看起来像Xamarin.Forms忽略了系统导航托盘,每个视图中的每个元素都显示在系统栏下方.我知道可以通过XAML.CS中的OnPlatform
stament或SizeChanged
事件来解决此问题,但是我想知道是否存在解决该问题的全局方法,而不是对每个页面进行少量修复.
Seems like Xamarin.Forms is ignoring the system navigation tray and every element in the each view appear under the system bar. I know this can be fixed with OnPlatform
stament or SizeChanged
event in the XAML.CS, but I want to know if exist a global approach to fix that issue instead making little fixes for each page.
看一看我的页面.
我的应用程序中几乎所有页面都用Grid
完成,因此这里有3行,第二行有2列;在iOS页面中可以完美运行...但是在android系统中,这确实有点痛苦.
Almost all pages in my app are done with Grid
, so here I have 3 rows, the second one with 2 columns; in iOS pages works perfectly... but android... it's a really somehow painful.
先谢谢了.
**从维护中添加的代码**
** ADDED CODE FROM MAINACTIVITY **
[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.
Notice I'm mainly iOS developer, so Android is a little hard to me to understand sometimes.
我的styles.xml
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项目中添加以下主题:
values/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,并向其中添加以下代码:
Create a folder named values-v21 and add an XML named styles.xml and add the following code to it :
<?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作为应用程序主题:
And use the name myTheme as the app theme in all your Android activities:
类似这样的东西:
[Activity(Label = "MyBankingApp", Icon = "@drawable/icon", Theme = "@style/MyTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, WindowSoftInputMode = SoftInput.AdjustResize, ScreenOrientation = ScreenOrientation.Portrait)]
在不起作用的情况下还原
Revert in case it doesn't work
这篇关于在Xamarin.Forms android中使用正确的屏幕高度和宽度的全局解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!