Android进度微调器,能够显示但不能以编程方式隐藏

Zug*_*dud 3 android

我正在尝试在我的testLayout.xml文件中指定一个进度微调器,然后以编程方式显示和隐藏它,就像我在下面的活动代码中所做的那样.然而,发生的事情是进度轮会显示,但即​​使我有setProgressBarVisibility(false)它也永远不会消失; 在末尾.有任何想法吗?谢谢你的意见

布局/ testLayout.xml

    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/ButtonNew"
        android:text="New" android:layout_alignParentBottom="true" />

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:id="@+id/list"
        android:layout_alignParentTop="true" android:layout_above="@id/ButtonNew" />

    <ProgressBar style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:id="@+id/progressBar1" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></ProgressBar>


</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

TestActivity.java

    public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                getWindow().requestFeature(Window.FEATURE_PROGRESS);
                setContentView(R.layout.testLayout);            
                setProgressBarVisibility(true);
    try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
setProgressBarVisibility(false);
    }
Run Code Online (Sandbox Code Playgroud)

Bob*_*ke4 7

好吧,你的代码中存在一个问题,因为你的Thread.sleep调用会导致onCreate在继续之前等待5秒.您永远不应该在主UI线程上执行Thread.sleep调用,因为它会导致应用程序在这段时间内没有响应.现在关于ProgressBar,您似乎没有正确地将setProgressBarVisibility与xml文件一起使用.这些函数应该用于标题栏中包含的ProgressBar.您应该使用findViewById(R.id.progressBar1).setVisibility(View.Gone)隐藏或View.Visible来显示.但如果这两个调用都是在onCreate内部完成的,那么你只能看到最后一个调用的调用.这是因为在从onResume和onCreate返回活动之前,实际上没有向用户显示ui.