2010-12-02 70 views
0

我想更新我的應用程序中的進度欄​​。在新視圖中更新進度欄

我已經通過使用inflater創建了一個新視圖,並且在新創建的視圖中我想顯示水平進度欄更新。

我該怎麼做,特別是?

另外我知道,當我們通過Inflater創建一個新的視圖時,我們需要通過addContentView()將它添加到當前活動類中,但我不知道如何做到這些,儘管我嘗試了很多直到現在。

有人可以幫我嗎?

回答

3

所以,因爲你沒有提供代碼,讓我搜索我的水晶球......等等......好的,它就是這樣。你有這樣的事情:

View someView = inflater.inflate(R.layout.view_with_progress_bar, null); 

爲了訪問ProgressBar,你必須使用findViewById方法:

ProgressBar yourProgressBar = (ProgressBar)someView.findViewById(R.id.id_of_your_progress_bar); 
// you can know modify the progress bar: yourProgressBar.setBlahBlah 

爲了添加包含進度條到你當前活動的視圖,您必須具有對您之前設置的容器的引用。所以,我想你以前做過:setContentView(R.layout.something);,那麼你有一個名爲something.xml的佈局;該版面包含ViewGroupLinearLayoutRelativeLayout等;我的水晶球看不清楚)。然後,你需要一個ID設置爲容器,創建一個引用,並添加新創建的視圖它:

// in your onCreate method 
setContentView(R.layout.something); 

// let's suppose it's a LinearLayout 
LinearLayout mainContainer = (LinearLayout)findViewById(R.id.id_you_gave_to_container); 

// blha blah... the rest of your code. Keep in mind that you will 
// probably have to declare the mainContainer outside the onCreate method 

// here, you have already inflated your view, and want to add it to your activity 
mainContainer.addView(someView); 
+0

對不起,不張貼我的代碼,並非常感謝您的回答(我真的相信你可能現在有一個水晶球,因爲我的代碼就像你所顯示的那樣),你能告訴我在哪裏我應該寫這行:「LinearLayout mainContainer =(LinearLayout)findViewById(R.id.id_you_gave_to_container);」因爲實際上我是在擴展BaseAdapter的類的Button的Click事件中執行我的代碼。 再次感謝Cristian。 – 2010-12-03 05:12:45