Android 图像将不会显示在contentview中
在Android 图像将不会显示在contentview中,android,multithreading,android-layout,xamarin,android-imageview,Android,Multithreading,Android Layout,Xamarin,Android Imageview,在Xamarin中,我有一个简单的活动,在加载另一个活动之前需要等待五秒钟 第一个活动有一个图像作为布局的一部分,但此图像不显示 这是我的活动代码: protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.SplashActivity); Thread.Sleep(5000); var testActi
Xamarin
中,我有一个简单的活动
,在加载另一个活动
之前需要等待五秒钟
第一个活动
有一个图像作为布局的一部分
,但此图像不显示
这是我的活动
代码:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.SplashActivity);
Thread.Sleep(5000);
var testActivity = new Intent (this, typeof(TestStartup));
StartActivity (testActivity);
}
这是我的SplashActivity
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_color">
<ImageView
android:src="@drawable/Splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/SplashImage"
android:layout_gravity="center_vertical" />
</LinearLayout>
应用程序等待五秒钟,然后加载TestStartup活动
,但是,在五秒钟的等待期间,图像不会显示。当活动
等待五秒钟时,如何显示此图像
我可以在这个代码上得到一些帮助吗
提前感谢它不会显示,因为您正在使用阻止UI线程
Thread.Sleep(5000);
你应该摆脱这种逻辑。依靠
x秒
总是错误的。您等待的计算可能已经准备就绪,使剩余的等待时间变为wast,或者可能需要更多的时间x秒
。要“修复”问题,可以使用Handler.postDelayed(Runnable,5000)
,并将需要执行的逻辑放入run
方法中。这样,至少UI线程不会被阻止您可能想看看Xamarin在Android中创建闪屏的教程:
看起来您的思路是正确的,但他们使用的是主题,而不是设置内容视图。如前所述,您的布局没有显示,因为您的睡眠方法阻塞了UI线程。使用一个主题是解决这个问题的一个好方法,你可以重用很多你已经拥有的东西