Android APP開發活用範例速查大辭典(Ch0106-ProgressBar自訂外觀「背景色、形狀、前景色」介紹)
ProgressBar自訂類型的XML
<?xml version=”1.0″ encoding=”utf-8″?> <layer-list xmlns:android=”http://schemas.android.com/apk/res/android” >
<!– ProgressBar的背景部分 –> <item android:id=”@android:id/background”> <shape> <gradient android:angle=”270″ android:endColor=”@color/FlatDarkCyan” android:startColor=”@color/FlatLightCyan” /> </shape> </item> <!– 顯示ProgressBar進度的部分 –> <item android:id=”@android:id/progress”> <clip> <shape> <gradient android:angle=”270″ android:endColor=”@color/FlatDarkBlue” android:startColor=”@color/FlatLightBlue” /> </shape> </clip> </item>
</layer-list>
|
GUI的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” >
<ProgressBar android:id=”@+id/progressbar” style=”?android:attr/progressBarStyleHorizontal” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:layout_centerInParent=”true” android:indeterminate=”false” android:max=”100″ android:progressDrawable=”@drawable/ch0106_pd_progress” />
<TextView android:id=”@+id/textview” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:layout_centerInParent=”true” android:textColor=”@color/FlatLightWhite” />
</RelativeLayout>
|
控制ProgressBar可以利用mHandler每10ms自動改變狀態
package jp.co.se.android.recipe.chapter01;
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.ProgressBar; import android.widget.TextView;
public class Ch0106 extends Activity { private Handler mHandler = new Handler(); private int value = 0; private ProgressBar mProgress; private TextView mTextView;
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ch0106_main);
mProgress = (ProgressBar) findViewById(R.id.progressbar); mTextView = (TextView) findViewById(R.id.textview);
// 進度的值持續增加(演示範例用) mHandler.post(addProgress); }
@Override protected void onDestroy() { // 捨棄執行中的Handler mHandler.removeCallbacksAndMessages(null); super.onDestroy(); }
/** * 進度的值每10ms增加 */ private Runnable addProgress = new Runnable() { @Override public void run() { value++; if (value > 100) { value = 0; } mProgress.setProgress(value); mTextView.setText(value + “%”); mHandler.postDelayed(addProgress, 10); } }; }
|