2011-12-17 82 views
0

我正在一個應用程序,我必須設置一些drawables。數字取決於用戶輸入。 因此,我需要在那裏使用一個循環,我設置了許多連續的圖像,如用戶在一行或多行中選擇的那樣。連續ImageViews在循環中

我找不出如何。我知道如何將圖像放在數組中,並使用 (ImageView)findViewById(R.id.img) 來設置它們。但我想在循環中創建這個連續的ImageViews。

回答

0

儘量使的LinearLayout和LayoutInflater的combinatino(膨脹的自定義視圖)

LinearLayout layout = (LinearLayout) findViewById(R.id.linear); 

    for (int i = 0; i < userChoice; i++) 
    { 
     LayoutInflater li = LayoutInflater.from(yourContext); 
     View customView = li.inflate(R.layout.image_holder, this); 
     // (In case you need to use the image) ImageView yourImage = (ImageView)customView.findViewById(R.id.image_view); 
     layout.addView(customView); 
    } 

其中:

userChoice是用戶選擇

佈局的數量是內部線性佈局的main.xml(或根元素本身)

image_holder是一個帶有ImageView的xml內部它

+0

非常感謝你!這有幫助。結合表格佈局中的行,解決了這個問題。 – Christer 2011-12-18 12:37:55