2012-06-24 86 views
5

我有這樣一個佈局:進度調用setVisible不工作

<?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方法我這樣做是爲了隱藏進度起初:

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

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

ProgressBar仍然是可見的所有時間...我也試過View.GONE

當我在XML文件中設置

android:visible="gone" 

,該ProgressBar犯規露面,但我不能讓它出現與

progressBar.setVisibility(View.Visible); 
+0

看起來它應該工作,你可以給一個(ProgressBar)findViewById(R.id.aPBar);' ,而不是'progressBar =(ProgressBar)layout.findViewById(使用增加)這個佈局視圖 –

+1

好吧,我得到它通過使用progressBar = (R.id.aPBar);'但我不知道爲什麼這會起作用。即時將這個佈局視圖添加到super.onCreate和setContentView之後的活動的onCreate中 – yon

回答

0

您正在使用膨脹佈局的新視角吹氣。這是不是當前在屏幕上的視圖。

因此,更改其可見性不會影響屏幕UI。

進一步提高您的活動,您必須呼叫setContentView這是在您的用戶界面上可見的佈局。

因此呼籲:

findViewById將返回你的進度條在屏幕上,但調用layout.findViewById將返回佈局進度條(正確),但不是您可以在屏幕上看到的進度。

0

這項工作對我來說:

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

解決方法對我來說是隱藏在XML視圖,然後取消隱藏/隱藏它在運行時需要的時候: android:visibility="gone"