在闪屏中插入加载环

问题描述 投票:0回答:1

我按照微软的教程在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>

在resources > values > styles.xml中的splash样式。

<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>

输出。闪屏

android xamarin loading splash-screen
1个回答
1
投票

你可以为闪屏创建一个布局,在该布局中创建一个图像和一个加载圆圈

splash_screen.xml活动代码

<?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>

在你的活动中,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


0
投票

基于 官样 ,我们可以创建一个 .xaml 布局 SplashActivity. 然后就可以实现了。

Splashlayout.xaml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_gravity="center"
        android:background="@drawable/splash_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

用于 SplashActivity :

//public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
//{
//    base.OnCreate(savedInstanceState, persistentState);
//    Log.Debug(TAG, "SplashActivity.OnCreate");
//}

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.Splashlayout);
}

在这里不要忘了修改 style.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">true</item>  
    <item name="android:windowContentOverlay">@null</item>  
    <item name="android:windowActionBar">true</item>  
  </style>

那么效果如下。

enter image description here

这里是 修改后的样板链接 以供参考

© www.soinside.com 2019 - 2024. All rights reserved.