我想用更少的代码数量在Android中的多个活动中添加相同的初始屏幕。
问题描述:
我想要在启动活动之前显示启动画面。为此,我创建了一个启动画面。但我的问题是,我在我的应用程序中有多个活动,现在我正在使用该单个启动画面。那么,要做到这一点的正确方法是,我必须将其逐个添加到所有活动中,或者可以一次完成相同的活动,那么该怎么做?这是什么方式?我想用更少的代码数量在Android中的多个活动中添加相同的初始屏幕。
答
详细说明我的评论,你很困惑与闪屏的目的。正如其名说,它应该只是出现在开始第二的飞溅和消失,进一步反馈给用户,以显示你必须使用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
以反映您的设计,其他所有将 保持不变。 因此没有这个任务的初始屏幕的概念,只是 进度条。
为什么要在加载任何活动之前显示启动画面?你甚至知道在Android中飞溅的目的? –
是的,我知道飞溅的目的,这就是为什么我使用它!我为我的应用使用了firebase,我的每项活动通常需要3-4秒才能从那里加载数据..这并不好。所以我用飞溅在那里加载缓存中的数据... – AnishKr
你绝对与闪屏混淆。 Splash用于应用程序的启动,以覆盖系统延迟以及一些初始应用程序设置。为了显示下载和其他繁重的任务,Android,iOS,Web有一些名为'ProgressBar'的东西,你一定可以给它一个坚实的背景,但它仍然会被称为进度条,并且会为你的目的出色。 –