Android的 - 用runOnUiThread做从一个线程的UI变化 [英] Android - using runOnUiThread to do UI changes from a thread
问题描述
我使用的是自定义标题视图,并想显示/隐藏在标题视图中的进度条,当一个线程的工作。
I am using a custom title view and would like to show/hide a progressbar in the title view when a thread is working.
这是我标题视图的XML
This is my title view's 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>
在我的活动,设置此为自定义标题栏后,我这样做
In my Activity, after setting this as the custom title bar, I do this
titleProgress = (ProgressBar)findViewById(R.id.progress_small_title);
titleProgress.setVisibility(View.INVISIBLE);
在这里titleProgress是进度的对象。
where titleProgress is ProgressBar object.
这是我做我的线程
runOnUiThread(new Runnable() {
public void run() {
titleProgress.setVisibility(View.VISIBLE);
}
});
//long operation here
runOnUiThread(new Runnable() {
public void run() {
titleProgress.setVisibility(View.INVISIBLE);
}
});
但没有改变的进度条。它永远不会显示。是否有人可以告诉我什么是错的code?
But there is no change to the progress bar. It is never displayed. Can someone please tell me what is wrong with the code?
是否有可能在一个自定义标题显示标题进度?
Is it possible to display the title progressbar in a custom title?
感谢。
推荐答案
几件事情要尝试:
1),(这可能是吧),确保 titleProgress
是易失。
1) (This probably isn't it) Make sure "titleProgress
" is volatile.
2)尽量扔几个 postInvalidate()
或 titleProgress.postInvalidate()
在那里触发重绘
2) Try throwing a few postInvalidate()
or titleProgress.postInvalidate()
in there to trigger a redraw.
3)你sacraficed一个x486计算机上的ALTER类似一个巨大的绿色机器人? (开玩笑)
3) Have you sacraficed a x486 machine on an alter resembling a giant green robot? (just kidding)
让我知道,如果这些前两个(如果你真的绝望了,第三个),让你在任何地方。
Let me know if those first two (and if you're really desperate, the third) get you anywhere.
这篇关于Android的 - 用runOnUiThread做从一个线程的UI变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!