进度条未在Android中显示

时间:2018-02-02 03:09:29

标签: android android-layout android-theme android-progressbar

我的应用程序有一个Splash Activity,在进行一些后台活动时向用户显示App徽标。我正在尝试提供一个进度条来显示后台活动的状态,让用户等待片刻。

的活动:

    public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    ProgressBar simpleProgressBar=(ProgressBar) findViewById(R.id.simpleProgressBar);
doProcessA();
simpleProgressBar.setProgress(10);
doProcessB();
simpleProgressBar.setProgress(20);
..

布局:

<RelativeLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
    android:id="@+id/simpleProgressBar"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:foregroundGravity="top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="170dp"
    android:background="@color/green"
    android:max="100"
    android:visibility="visible" />
</RelativeLayout>

我的Splash活动主题如下:

<activity
  android:name=".SplashActivity"
  android:theme="@style/SplashTheme"
  android:configChanges="orientation|keyboardHidden|screenSize"
  android:screenOrientation="portrait">
  <intent-filter>
     <action android:name="android.intent.action.MAIN" />
     <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

<style name="SplashTheme" parent="AppTheme">
    <item name="android:windowBackground">@drawable/splash_drawable</item>
</style>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="transparent">
    <item android:drawable="@color/colorPrimary" />
    <item>
        <bitmap android:src="@drawable/app_logo" />
    </item>
</layer-list>

我想在应用徽标下方显示进度条。但是,我只是无法让进度条显示在屏幕上。

3 个答案:

答案 0 :(得分:0)

试试这个,

您需要在时间间隔内给出进度条时间,使您的代码符合您的预期。

首先初始化进度条状态(进度条的时间)

int progressStatus = 0;
Handler handler = new Handler();

然后在create method

里面
new Thread(new Runnable() {
        @Override
        public void run() {
            while (progressStatus < 100) { 
                progressStatus += 1;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setProgress(progressStatus);

                    }
                });
                try {
                    Thread.sleep(30);  // total time for progress bar (milli seconds//
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (progressStatus == 100) {
                 // do your code here //
                } 

            }
    }).start();

答案 1 :(得分:0)

应该是你的布局设置,我不知道你为什么要将ProgressBar宽度设置为android:layout_width =&#34; match_parent&#34;

答案 2 :(得分:0)

试试这个

public class SplashScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    ProgressBar simpleProgressBar=(ProgressBar) 
        findViewById(R.id.simpleProgressBar);
    int SPLASH_TIME_OUT = 3000;
    new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity

        doProcessA();
        simpleProgressBar.setProgress(10);
        doProcessB();
        simpleProgressBar.setProgress(20);
            }

            // close this activity
            finish();
        }
    }, SPLASH_TIME_OUT);
}
}

Handler线程内的所有内容。它会起作用