2009-05-23 66 views
24

我正在使用自定義標題視圖,並希望在線程工作時在標題視圖中顯示/隱藏進度條。Android - 使用runOnUiThread從線程執行UI更改

這是我的標題視圖的XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 
    <Button 
     android:id="@+id/image_left_btn" 
     android:layout_width="75dip" 
     android:layout_height="wrap_content" 
     android:text="Back" 
    /> 
    <TextView 
     android:id="@+id/image_title_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textSize="20dip" 
     android:textStyle="bold" 
     android:textColor="#fff" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:paddingLeft="8dip" 
     android:paddingRight="8dip" 
    /> 
    <ProgressBar 
     android:id="@+android:id/progress_small_title" 
     style="?android:attr/progressBarStyleSmall" 
     android:layout_width="75dip" 
     android:layout_height="wrap_content" 
     android:paddingRight="8dip"/> 
</LinearLayout> 

在我的活動,設置此爲自定義標題欄後,我做這個

titleProgress = (ProgressBar)findViewById(R.id.progress_small_title); 
titleProgress.setVisibility(View.INVISIBLE); 

其中titleProgress是進度的對象。

這是我在我的線程

runOnUiThread(new Runnable() { 
    public void run() { 
     titleProgress.setVisibility(View.VISIBLE); 
    } 
}); 
//long operation here 
runOnUiThread(new Runnable() { 
    public void run() { 
     titleProgress.setVisibility(View.INVISIBLE); 
    } 
}); 

做但有進度條沒有變化。它從不顯示。有人可以告訴我什麼是錯誤的代碼?

是否可以在自定義標題中顯示標題進度條?

謝謝。

回答

16

幾件事情要嘗試:

1)(這大概是吧),確保 「titleProgress」 是易揮發。

2)嘗試投擲幾個postInvalidate()titleProgress.postInvalidate()在那裏觸發重繪。

3)你有沒有像一個巨大的綠色機器人改變x486機? (只是在開玩笑)

讓我知道如果那些前兩(如果你真的絕望,第三)讓你到達任何地方。

+3

已經嘗試了前兩個,但沒有幫助!而絕望,我是這樣想的,要求3? ;) – lostInTransit 2009-09-23 08:01:21

+0

3)選項是解決所有問題的最佳解決方案...... LOL – Rodrigo 2016-04-22 20:41:05

0

試着改變進度的風格臥式:

style="?android:attr/progressBarStyleHorizontal" 

原來

style="?android:attr/progressBarStyleSmall" 

給出了一個圓形的「進度」,它不能在標題欄完全可見。

+0

我甚至試着用progressBarStyleSmallTitle,它應該適合默認的標題欄。即使這不表明。 – lostInTransit 2009-06-06 13:39:25

1

您的佈局是否正確?你是否試圖首先使用垂直佈局來進行這項工作?只是爲了查看當你開始你的活動時進度條是否可見?

4

使用通訊與處理程序,

  1. 執行你的線程
  2. run():你應該將消息發送給處理程序將更新進度條(當然水平):handler.sendMessage(handler.obtainMessage());
  3. 您活動,你應該覆蓋方法handleMessage(Message msg):像這樣

    handler = new Handler(){ 
        @override 
        public void handleMessage(Message msg) 
        { 
        //here you write the code which will update your progressBar 
        } 
    };