2009-05-23 35 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 
        } 
    };