gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:40:16 24 4
gpt4 key购买 nike

我正在使用适用于 Android 和 iOS 平台的 Xamarin.Forms 为客户端开发一个新应用程序,一切顺利,因为我决定在 Android 平台上以不同的屏幕尺寸审查该应用程序。

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

看看我的一个页面。

我的应用程序中几乎所有页面都是使用 Grid 完成的,所以这里我有 3 行,第二个有 2 列;在 iOS 页面中运行完美...但是在 Android 中...这真的有点痛苦。

提前致谢。

** 从 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 有时对我来说有点难以理解。

我的样式.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

最佳答案

  • 在您的 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 并向其中添加以下代码:

     <?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 Activity 中使用名称 myTheme 作为应用主题:

像这样:

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

如果它不起作用则还原

关于android - 在 Xamarin.Forms android 中使用正确屏幕高度和宽度的全局解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49365764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com