进度条设置可见性无效

7

我有这样一个布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
android:background="@drawable/menu_background"
>

<ProgressBar 
  android:id="@+id/aPBar"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  style="@android:style/Widget.ProgressBar.Inverse"/>

 ...

</RelativeLayout>

在我的 onCreate 方法中,我首先执行以下操作来隐藏 ProgressBar:

LayoutInflater inflater = getLayoutInflater();
RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.achievements_screen, null);

progressBar=(ProgressBar)layout.findViewById(R.id.aPBar);
progressBar.setVisibility(View.INVISIBLE);

但是进度条始终可见...我也尝试过使用View.GONE。当我设置:
android:visible="gone"

在XML文件中,ProgressBar没有显示出来,但我无法使其出现。
 progressBar.setVisibility(View.Visible);

看起来应该可以工作,你能否再详细说明一下你在哪里使用(添加)这个布局视图? - Dheeresh Singh
1
好的,我通过使用progressBar=(ProgressBar)findViewById(R.id.aPBar);而不是progressBar=(ProgressBar)layout.findViewById(R.id.aPBar);使其工作了,但我不知道为什么会起作用。在super.onCreatesetContentView之后,在我的活动的onCreate中添加了此布局视图。 - yon
3个回答

2
您正在使用布局填充器(Layout Inflater)膨胀(inflate)一个新的视图。这 不是 当前显示在屏幕上的视图。
因此,更改其可见性不会影响屏幕用户界面。
在您的 Activity 中,您必须调用了 setContentView,并且这是在您的用户界面上可见的布局。
因此,调用: findViewById 将返回屏幕上的进度条,但调用 layout.findViewById 将返回该布局中的进度条(正确),但那不是您在屏幕上看到的 progressBar。

0
我的解决方法是在XML中隐藏视图,然后在需要时在运行时取消隐藏/隐藏它: android:visibility="gone"

0

这对我有用:

rootView.findViewById(R.id.progress_bar).setVisibility(View.GONE);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接