Android Studio进度条移动

时间:2015-11-07 18:24:44

标签: android

我在我的android工作室项目中放了一个进度条,并希望它移动,但我不知道该怎么做。我试图在YouTube上寻找教程并继续学习,但这并不适合我。然后我看了谷歌的教程,但这对我没有用。当我搜索它们时,我只看到带有圆形加载条的教程,我希望我的水平。我只知道从哪里开始加载栏但我真的想知道如何让它移动然后进入游戏。我没有任何代码,因为当我看到它没有工作时我就删除了它。如果有人有一个很好的教程或代码可以帮助我,我会非常感激。感谢。

4 个答案:

答案 0 :(得分:1)

可以非常简单地在整个代码中使用进度条

<强> custom_progressbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progressbar_view"
        android:layout_width="@dimen/_70sdp"
        android:layout_height="@dimen/_70sdp"
        android:indeterminate="true"
        android:padding="@dimen/_10sdp" />

</RelativeLayout>

<强> CustomProgressBar.java

public class CustomProgressBar extends Dialog {

    Activity activity;

    public CustomProgressBar(Activity act) {
        super(act);
        this.activity = act;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.custom_progressbar);

        setCancelable(false);

//        set backgroung transparent
        getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    }

}

并在您需要活动或片段时应用

   public CustomProgressBar customProgressBar;
  customProgressBar = new CustomProgressBar(context);

 customProgressBar.show();

答案 1 :(得分:0)

检查:事实上,您必须为水平进度条移动编码并设置其进度,它专门用于下载以显示操作完成的百分比。

XML代码:

      <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="23dp"
            android:layout_marginTop="20dp"
            android:indeterminate="false"
            android:max="100"
            android:minHeight="50dp"
            android:minWidth="200dp"
            android:progress="1" />

java代码:

    public class MainActivity extends Activity {
     private ProgressBar progressBar;
     private int progressStatus = 0;
     private TextView textView;
     private Handler handler = new Handler();

     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      progressBar = (ProgressBar) findViewById(R.id.progressBar1);
      textView = (TextView) findViewById(R.id.textView1);
     //Long operation by thread
      new Thread(new Runnable() {
         public void run() {
            while (progressStatus < 100) {
               progressStatus += 1;
      //Update progress bar with completion of operation
        handler.post(new Runnable() {
        public void run() {
           progressBar.setProgress(progressStatus);
           textView.setText(progressStatus+"/"+progressBar.getMax());
        }
            });
            try {
               // Sleep for 200 milliseconds. 
                             //Just to display the progress slowly
               Thread.sleep(200);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
         }
      }
      }).start();
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items 
                    //to the action bar if it is present.
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
     }
    }

答案 2 :(得分:0)

.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"
    android:weightSum="50">
<ProgressBar

    style="?android:attr/progressBarStyleHorizontal"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:indeterminate="false"

    android:id="@+id/Bar1"

    android:max="10"

    android:padding="15dp"
    android:paddingTop="5dp"
    android:paddingRight="20dp"
    android:paddingLeft="20dp">

    </ProgressBar>



<Button

    android:id="@+id/button1"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:onClick="startProgress"

    android:text="Start" />

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar2"
        android:layout_gravity="center_horizontal" />


</LinearLayout>

MainActivity.java

    package com.example.dell.vlakna;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

import static com.example.dell.vlakna.R.layout.activity_main;

public class MainActivity extends Activity {
    private ProgressBar bar;

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_main);
        bar = (ProgressBar) findViewById(R.id.Bar1);

    }

    public void startProgress(View view) {

        bar.setProgress(0);
        new Thread(new Task()).start();
    }

    class Task implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i <= 10; i++) {
                final int value = i;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                bar.setProgress(value);

            }
        }

    }

}

这段代码运行良好。

答案 3 :(得分:0)

public class MainActivity extends AppCompatActivity {

private ProgressBar progressBar;
private TextView loading;

private int progressStatus = 0;

private Handler pHandler = new Handler();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    loading = (TextView) findViewById(R.id.loading);

    new Thread(new Runnable() {
        @Override
        public void run() {
            while (progressStatus < 100) {
                progressStatus ++;
                android.os.SystemClock.sleep(50);
                pHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setProgress(progressStatus);
                    }
                });

            }
            pHandler.post(new Runnable() {
                @Override
                public void run() {
                    loading.setText("COMPLETE");
                }
            });
        }
    }).start();

}

此代码有效。您应该能够为ProgressBar使用默认的XML代码。您将需要添加元素,例如视觉设计中的实际进度栏。我认为您需要在末尾添加一个额外的'}'。