Android 基於幀布局實現一個進度條 FrameLayout+ProgressBar

來源:互聯網
上載者:User

標籤:

在FrameLayout中添加一個ProgressBar置中

<ProgressBar        android:layout_gravity="center"        android:id="@+id/progressBar1"        style="?android:attr/progressBarStyleLarge"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />

添加一個TextView置中

<TextView        android:layout_gravity="center"        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="0%" />

該程式實現了一個跑動的進度條,使用了多線程,參考自:http://www.cnblogs.com/tyjsjl/p/4014285.html

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="使用幀布局(FrameLayout)實現一個ProgressBar的動態效果" />    <ProgressBar        android:layout_gravity="center"        android:id="@+id/progressBar1"        style="?android:attr/progressBarStyleLarge"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:layout_gravity="center"        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="0%" /></FrameLayout>
activity_main.xml
package com.example.runprogressbar;import java.util.Timer;import java.util.TimerTask;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.ActionBarActivity;import android.widget.TextView;public class MainActivity extends ActionBarActivity {        private TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                textView = (TextView) findViewById(R.id.textView1);        new Thread(new MyThread()).start();    }        Handler handler = new Handler() {        public void handleMessage(Message msg) {            textView.setText(msg.what + "%");        }    };        class MyThread implements Runnable {                private int percentNum = 0;        @Override        public void run() {            while (true) {                try {                    Thread.sleep(1000);                    Message msg = new Message();                    msg.what = ++percentNum;                    handler.sendMessage(msg);                    if (percentNum >= 100)                        break;                } catch (InterruptedException e) {                    e.printStackTrace();                }                            }        }            }}
MainActivity.java

 效果:

Android 基於幀布局實現一個進度條 FrameLayout+ProgressBar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.