问题描述
我的应用在加载时显示启动画面.我想在初始屏幕上的图标下方放置一个动画进度条.我尝试使用 XML,但它崩溃了!说无效的标签进度条.
My app displays a splash screen while the app is loading. I want to put an animated progress bar below the icon on the splash screen. I tried using XML, but it's crashing! Says invalid tag progressbar.
这是我在styles.xml中调用启动画面的代码
Here's my code to call the splash screen, in styles.xml
<style name="AppTheme.BrandedLaunch" parent="AppThemeDark">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/background_splash</item>
</style>
这是我的 background_splash.xml 文件
Here's my background_splash.xml file
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/splash_screen_bg"/>
<item>
<bitmap
android:gravity="center"
android:tileMode="disabled"
android:src="@drawable/ic_launcher"/>
</item>
<item>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</item>
</layer-list>
我真的不想使用不同的方法制作启动画面,因为这种方法非常简单.有什么想法吗?
I don't really want to make a splash screen using a different method, because this method was really simple. Any ideas?
推荐答案
关键在本教程:http://www.logicchip.com/android-splash-screen-progress-bar/您必须在 drawable 文件夹中创建 un 文件:例如 splas_screen.xml
the key is in this tutorial:http://www.logicchip.com/android-splash-screen-progress-bar/You must create un file in drawable folder: example splas_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp">
<item>
<color android:color="@color/main_color_grey_50"/>
</item>
<item>
<bitmap
android:gravity="center"
android:filter="true"
android:src="@drawable/logomies"
/>
</item>
</layer-list>
向文件 styles.xml 添加新样式
Add new Style to file styles.xml
<style name="SplashTheme" parent ="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_screen</item>
</style>
AndroidManifest 文件,您应该像设置 LAUNCHER Activity 一样设置 SplashActivity.
AndroidManifest file you should set the SplashActivity like your LAUNCHER Activity.
<application
android:name=".ActBenApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/ActivityTheme">
<activity android:name=".main.ui.SplashActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".main.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
</activity>
</application>
重要的部分来了:你必须在进度条所在的位置创建一个布局,这个布局没有背景,所以进度条出现在斜线前面.
The important part comes here: you must create a layout where the progress bar will be, this layout has no background so the progress bar appears forward of slash.
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/splash_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.ui.SplashActivity">
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="visible"
android:indeterminateOnly="true"/>
</android.support.constraint.ConstraintLayout>
最后在您的 Activity.java 文件中:
Finally in your Activity.java file:
public class SplashActivity extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, 3000);
}
}
将 setContentView 设置为创建的布局.就是这样..!
Set as the setContentView to the layout created. That is all..!
这篇关于android - 启动画面上的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!