[英]Android Splash Activity not showing (Xamarin Forms)
My Splash Activity just shows a plain white screen. 我的启动活动仅显示纯白色屏幕。 I am in a Xamarin Forms project. 我在Xamarin Forms项目中。 When I tried using a style to set the background, it worked, but the image was stretched out, which is why I am trying to use a LinearLayout and ImageView instead. 当我尝试使用一种样式设置背景时,它可以工作,但是图像被拉长了,这就是为什么我尝试使用LinearLayout和ImageView代替的原因。 This is my code: 这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace Buddy.Droid
{
[Activity(Label = "HeyBuddy", Icon = "@drawable/icon", Theme = "@style/splashscreen", MainLauncher = true, NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
var imageView = new ImageView(this);
imageView.SetImageResource(Resource.Drawable.splash_screen);
var layout = new LinearLayout(this);
layout.SetBackgroundColor(Color.ParseColor("#0098CC"));
layout.AddView(imageView);
SetContentView(layout);
}
protected override void OnResume()
{
base.OnResume();
StartActivity(typeof(MainActivity));
}
}
}
To programmatically create a Layout
with an ImageView
: 要以编程方式使用ImageView
创建Layout
:
ViewGroup.LayoutParams
with its width and height set to MatchParent
创建一个ViewGroup.LayoutParams
并将其宽度和高度设置为MatchParent
ImageView
将layoutparams分配给您的ImageView
ImageView.SetScaleType
for how you want to scale your drawable (ie ImageView.ScaleType.FitCenter
) 为想要缩放可绘制对象的方式分配一个ImageView.SetScaleType
(即ImageView.ScaleType.FitCenter
) Note: You do not need the LinearLayout, just set the background color of your ImageView 注意:您不需要LinearLayout,只需设置ImageView的背景颜色
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
var imageView = new ImageView(this)
{
LayoutParameters = layoutParams
};
imageView.SetImageResource(Resource.Drawable.splash_screen);
imageView.SetScaleType(ImageView.ScaleType.FitCenter);
imageView.SetBackgroundColor(Color.ParseColor("#0098CC"));
SetContentView(imageView);
}
protected async override void OnResume()
{
base.OnResume();
await Task.Delay(5000); // simulate some background work....
StartActivity(typeof(MainActivity));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.