【问题标题】:Xamarin forms custom splash screenXamarin 形成自定义启动画面
【发布时间】:2020-05-11 21:35:17
【问题描述】:

在我的启动画面上,我想将适用于 Android 和 iOS 的 App 版本放在底部

【问题讨论】:

  • 为什么不在分片代码上创建一个专门的启动画面?

标签: android ios xamarin.forms xamarin.android xamarin.ios


【解决方案1】:

这就是我在 Xamarin Forms 应用程序中添加启动画面的方式

  1. 创建一个新的 ContentPage 即 Splash.xaml
  2. 将 App.xaml.cs 文件中的 MainPage 设置为 MainPage = new Splash();
  3. 在 Splash.xaml 中,我添加了一个图像(徽标)并将 ContentPage 背景色设置为我想要的应用颜色。
  4. 在图像下方的 Splash.xaml 上添加标签
  5. 在 Splash.xaml.cs OnAppearing() 函数中,将 Label 的 Text = 设置为应用的当前版本。您可以使用 Xamarin.Essentials 插件来获取版本。 (可选)然后,我运行一个任务来为徽标设置动画或淡入/淡出。在 Splash.xaml.cs OnAppearing 函数中完成动画后,我将 MainPage 设置为新页面,即。登录页面或 Main.xaml
  6. 在 Android 项目中,我将 MainActivity "MainLauncher = true" 保持原样。
  7. 在 iOS 项目中,我从 StoryBoard 中删除了 Xamarin 徽标,并将背景颜色设置为我想要的应用颜色。

这是我网站上的一些sample code

【讨论】:

  • 因为这是你自己的网站,你must disclose your affiliation in the answer为了不被视为垃圾邮件。根据 SO 政策,在您的用户名中使用与 URL 相同的文本或在您的个人资料中提及它不被视为充分披露。
【解决方案2】:

要制作自定义启动画面,试试这个

在 Droid 项目的资源目录中创建 SplashScreen.axml

`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
         <ImageView
          .
          .PLACE YOUR IMAGE CODE HERE
          .
         />
    <TextView
        android:id="@+id/AppVersion"
        android:text="App-Version - "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="40dp"
        android:layout_marginBottom="40dp"
        android:textColor="#FFFFFF"
        android:textSize="12"
        android:background="@android:color/transparent" />
</RelativeLayout>`

然后在 Droid 项目中为你的启动画面创建 cs 文件

    namespace Blue.Test {
    [Activity(Theme = "@android:style/Theme.NoTitleBar", MainLauncher = true, NoHistory = true, ScreenOrientation = ScreenOrientation.Portrait)]    
    public class SplashScreenActivity : Activity {
        protected override void OnCreate(Bundle savedInstanceState) {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.SplashScreen);            
            FindViewById<TextView>(Resource.Id.AppVersion).Text = $"Version {PackageManager.GetPackageInfo(PackageName, 0).VersionName}";            
        }
    }
}

记得在你的 assemble 中设置MainLauncher = true &amp; NoHistory = true

【讨论】:

  • 你有这方面的样品吗,我以前试过,但它不起作用
【解决方案3】:

通常,我们使用可绘制资源来创建启动画面。无法输入文字,你可以用你感兴趣的文字生成静态图片。

我们用来创建启动画面的方式。

Resource> Drawable> 创建 splash_background.xml

<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <color android:color="#000000"/>
  </item>
 <item>
   <bitmap
    android:src="@drawable/pink"
    android:tileMode="disabled"
    android:gravity="center"/>
  </item>
</layer-list>

Resources> values> 在styles.xml 中添加样式

<style name="MyTheme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="android:windowBackground">@drawable/splash_background</item>
</style>

在 MainActivity 中更改主题。

[Activity(Label = "SplashScreenDemo", Icon = "@mipmap/icon", Theme = "@style/MyTheme.Splash", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

有关 IOS 的更多信息,您可以查看链接。 https://medium.com/@thesultanster/xamarin-splash-screens-the-right-way-3d206120726d

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多