!I've implemented this splashscreen in android using android studio, but I have switched to xamarin forms for cross-platform, so I want to implement a similar splashscreen in xamarin forms] 1
到目前为止,我已经能够添加徽标,但是我需要添加文本,该怎么做?
这是我的xamarin形式的splash.xml代码:
<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="@color/colorBlack"/>
</item>
<item>
<bitmap
android:src="@drawable/ic_logo"
android:tileMode="disabled"
android:gravity="center"/>
</item>
</layer-list>
默认的SplahScreen不支持添加文本。
解决它的快速方法是到include those version info inside your image
。
另一种方法是create a Activity that represents your splash screen
。然后,在创建SplashActivity时,将启动一个持续3秒的计时器。经过3秒后,您只需启动应用程序的主要活动即可。
SplashActivity
的布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:background="#fff"
android:id="@+id/splashview">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src = "@drawable/splash_logo"
/>
<TextView
android:id="@+id/txtAppVersion"
android:text="test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="30dp"
android:layout_marginBottom="30dp"
android:textColor="#000"
android:textSize="24sp"
android:gravity= "bottom"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
和后面的代码:
[Activity(Theme = "@style/MyTheme.Splash", MainLauncher =true,NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
static readonly string TAG = "X:" + typeof (SplashActivity).Name;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Splash);
FindViewById<TextView>(Resource.Id.txtAppVersion).Text = $"Version {PackageManager.GetPackageInfo(PackageName, 0).VersionName}";
}
// Launches the startup task
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
}
// Prevent the back button from canceling the startup process
public override void OnBackPressed() { }
// Simulates background work that happens behind the splash screen
async void SimulateStartup()
{
Log.Debug(TAG, "Performing some startup work that takes a bit of time.");
await Task.Delay(5000); // Simulate a bit of startup work.
Log.Debug(TAG, "Startup work is finished - starting MainActivity.");
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
我上传了一个示例项目here,您可以检查。