android - 在启动画面中插入加载环

标签 android xamarin loading splash-screen

我按照 Microsoft 教程了解如何在 xamarin.android 中制作启动屏幕,并且它工作正常。现在我想在位图下插入一个进度环。我尝试过不同的方法,但似乎找不到正确的方法

这是启动屏幕代码:

splash_screen.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <color android:color="@color/splash_background"/>

  </item>

  <item android:left="20dp">
    <bitmap
        android:src="@mipmap/coronamap_bianco"
        android:tileMode="disabled"
        android:gravity="center"/>
  </item>

</layer-list>

资源中的启动样式>值> styles.xml

<style name="MyTheme.Splash" parent ="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/splash_screen</item>
    <item name="android:windowNoTitle">true</item>  
    <item name="android:windowFullscreen">false</item>  
    <item name="android:windowContentOverlay">@null</item>  
    <item name="android:windowActionBar">true</item>
    <item name="android:statusBarColor">#304057</item>
  </style>

SplashActivity.cs

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
    public class SplashActivity : AppCompatActivity
    {
        public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
        {
            base.OnCreate(savedInstanceState, persistentState);

        }

        // Launches the startup task
        protected override void OnResume()
        {
            base.OnResume();
            StartActivity(new Intent(Application.Context, typeof(MainActivity)));
            Finish();
        }

    }

编辑

由于约束层不起作用,我尝试使用线性布局来简化它。作为输出,我得到一个白屏。

SplashActivity.cs

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
    public class SplashActivity : AppCompatActivity
    {
        public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
        {
            base.OnCreate(savedInstanceState, persistentState);
            SetContentView(Resource.Layout.SplashLayout);

        }

        // Launches the startup task
        protected override void OnResume()
        {
            base.OnResume();
            StartActivity(new Intent(Application.Context, typeof(MainActivity)));
            Finish();
        }   
    }

SplashLayout.xml

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_splash_screeen"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/coronamap_bianco" />

    <ProgressBar
        android:id="@+id/progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp" />

</LinearLayout>

输出:splash screen

最佳答案

您可以为启动屏幕创建一个布局,在该布局中创建一个图像和一个加载圆圈

splash_screen.xml Activity 代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv_splash_screeen"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.45"
        app:srcCompat="@drawable/my_logo" />

    <ProgressBar
        android:id="@+id/progress_bar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/iv_splash_screeen" />

</androidx.constraintlayout.widget.ConstraintLayout>

在您的 Activity 中,OnCreate 设置该布局 SplashActivity.cs

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
    public class SplashActivity : AppCompatActivity
    {
        public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
        {
            base.OnCreate(savedInstanceState, persistentState);

      // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.splash_screen);
        }

     .....
      ...

    }

输出:

enter image description here

关于android - 在启动画面中插入加载环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62389278/

相关文章:

c# - Xamarin 形成带有图像的 ListView |文字 |时间

javascript - 根据需要加载多个 JavaScript 与一次加载一个大文件

android - 使用 HC-05 将连续数据从 arduino 发送到 Android 应用程序

android - 如何在sqlite3中获取带有查询数据的列名?

在 SDK rev 18 更新中找不到基于 Android x86 的系统镜像

android - 让 MvxAutoCompleteTextView 工作

java - 在android中顺序上传文件更安全/更好还是用多个线程同时上传文件更好?

c# - 隐藏的 Xamarin ActionBar 按钮项

jquery - 如何向此弹出窗口添加加载 gif,然后将其删除?

c++ - .dll 文件是为每个程序加载一次还是为所有程序加载一次?