我想在Android中的多个活动中使用较少的代码添加相同的启动画面。

时间:2017-05-06 03:24:18

标签: android splash-screen

我希望在打开活动之前显示启动画面。为此我创建了一个闪屏。但我的问题是我在我的应用程序中有多个活动,现在我正在使用那个单一的启动画面。所以,正确的方法是,我必须逐个将它添加到所有活动中,或者可以一次完成,然后如何?那是什么方式?。

1 个答案:

答案 0 :(得分:0)

在那里阐述我的评论,你对Splash Screen的目的感到困惑。正如名称所示,它应该只是在开始时显示为秒,并且消失,以便向用户提供进一步的反馈以显示您必须使用的下载ProgressBar

您可以在此处将ProgressBar的代码外部化为最少的代码。

<强> progress_bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <!-- Customize yoiur progress bar screen here--> 
</RelativeLayout>

然后在任何地方包含此布局

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.android.myApp.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <include
        android:id="@+id/progress_bar"
        android:visibility="gone"
        layout="@layout/progress_bar_layout" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

然后在你的java类

private RelativeLayout progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //some code
    progressBar = (RelativeLayout)findViewById(R.id.progress_bar);
    //control parent instead of progress bar only
}

private void beginDownload(){
    progressBar.setVisibility(View.VISIBLE);
    //your download code
}

private void downloadComplete(){
    progressBar.setVisibility(View.GONE);
    //codes once download is completed
}
  

P.S。如果您的设计特别需要一些不同的屏幕   比旋转进度条,然后使用相同的方法,只需更改   progress_bar_layout.xml反映您的设计,其他一切都会   保持不变。 因此没有关于此任务的启动屏幕概念   进度条。