2011-06-18 67 views
0

在我的活動中,我有大約40個線性佈局...基本上5個父母和其他孩子的父母。我有多個線性佈局,我怎麼能一次改變一些或所有的背景,然後再改變它們,並再次改變它們?

我想要做的是改變一些背景,然後點擊一個按鈕並改變其他背景。

我遇到的問題...不幸的是,我現在沒有我的確切代碼是,一旦我改變背景,我必須將其設置爲空。

,我在我的Java類的代碼看起來是這樣的......

mlayout1= findViewById(R.id.layout1); 

mlayout2= findViewById(R.id.layout2); 

mlayout3= findViewById(R.id.layout3); 

mlayout4= findViewById(R.id.layout3); 

if variable = 1{ 

mlayout1.setBackgroundResource(R.drawable.background_img); 
mlayout2.setBackgroundResource(R.drawable.background_img); 
mlayout3.setBackgroundResource(R.drawable.background_img); 
mlayout4.setBackgroundResource(R.drawable.background_img); 
} 

if variable = 2{ 

mlayout1.setBackgroundResource(R.drawable.background_img); 

mlayout2.setBackgroundResource(null); 

mlayout3.setBackgroundResource(R.drawable.background_img); 

mlayout4.setBackgroundResource(null); 
} 

if variable = 3{ 

mlayout1.setBackgroundResource(R.drawable.background_img); 

mlayout2.setBackgroundResource(null); 

mlayout3.setBackgroundResource(null); 

mlayout4.setBackgroundResource(null); 
} 

等..

的一個問題是,有大約30的佈局,我需要改變我正在處理的背景以及大約500種不同的組合...

我遇到的最大問題是必須在應用新的背景組合之前將所有背景重新設置爲null ...

我有一張桌子,裏面有所有不同的組合。

我希望能夠通過點擊下一個和/或上一個按鈕或輸入組合ID號碼來滾動瀏覽它們,並讓正確的背景組合點亮,然後在點亮下一個之前使它們全部復位組合......

也許我需要另一個活動來重新設置一切爲空,然後再繪製新的背景組合或我不確定如何有效地做到這一點。

我是新來的Android和我的頭很痛,因爲我一直在試圖弄清楚這一點,現在周...

+0

我知道這是做錯的方法......我試圖找出正確的方法......我已經看了教程,並觀看了幾個小時的視頻教程,我也找不到任何想法如何做到這一點。我想改變背景,然後再改變它們,並且一次又一次地...... – EltMrx

回答

1

你可能想要做的第一件事就是把你的佈局圖到一個數組或列表或東西,所以你不必在每一個的ID名稱代碼:

public void onCreate(Bundle b) { 
    setContentView(R.layout.main); 

    List<View> myViews = new ArrayList<View>(); 
    myViews.add(findViewById(R.id.layout1)); 
    myViews.add(findViewById(R.id.layout2)); 
    // etc. 
} 

然後你就可以直接和編程方式訪問它們在數組中。但這仍然是很多代碼。

如果你有機會獲得他們的父母在做出選擇時,你可以遍歷它的孩子:

public void performSelection(int selection) { 
    // One of your parents that holds a lot of child views 
    ViewGroup parent = (ViewGroup)findViewById(R.id.parent_1); 

    for(int i = 0; i < parent.getChildCount(); i++) { 
     View v = parent.getChildAt(i); 

     // If selection is 1, this sets the drawable background on every item. 
     // If 2, then sets drawable on 0, 2, 4, 6, ... and null on 1, 3, 5, ... 
     // If 3, then sets drawable on 0, 3, 6, 9, ... and null on 1, 2, 4, 5, ... 
     if (i % selection == 0) { 
      v.setBackgroundResource(R.drawable.background_img); 
     } else { 
      v.setBackgroundResource(null); 
     } 
    } 
} 

小心,這將着眼於每一個孩子的請求家長。確保它只包含您想要更改背景的視圖。

另一種方法:如果佈局視圖在XML中基本相同但ID不同,則可以考慮爲其創建單獨的XML資源,然後在創建活動時動態地將它們加載到您的父母或數組中。在這種情況下,您的主佈局XML文件將具有parent_1元素,但沒有子視圖,因爲它們將在運行時添加。

layout/one_element.xml

<LinearLayout ...> 
    <TextView android:id="@+id/element_text" ... /> 
    <!-- etc --> 
</LinearLayout> 

然後,你可以做這樣的事情:

public void onCreate(Bundle b) { 
    setContentView(R.layout.main); 

    // One of your parents that will hold a lot of child views 
    ViewGroup parent = (ViewGroup)findViewById(R.id.parent_1); 

    for (int i = 0; i < NUM_CHILDREN; i++) { 
     View newView = getLayoutInflater().inflate(R.layout.one_element, 
                null); 
     // Set anything specific to the new view 
     // eg. 
     TextView tv = (TextView)newView.findViewById(R.id.element_text); 
     tv.setText(String.format("Hi, I'm element %d!", i)); 

     // Add it to the parent 
     parent.addView(newView); 
    } 
} 

然後你就可以使用上面的performSelection方法需要改變的背景。

相關問題