2013-01-23 58 views
0

我想知道如何根據條件更改xml文件的佈局。因此,可以說,我們有一個佈局等..如何切換xml佈局排序

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/app_background" 
android:padding="5dip" 
> 

<ListView android:id="@+id/xlist" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:cacheColorHint="#00000000" 

      android:divider="@drawable/listdivider" 
      android:dividerHeight="19dp" 

      /> 
    <TextView 
       android:layout_width="fill_parent" 
       android:background="@drawable/listdivider" 
       android:layout_height="19dp" 
       android:visibility="gone" 
      android:id="@+id/dividerline" 
       /> 
    <ListView android:id="@+id/ylist" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:cacheColorHint="#00000000" 
      android:divider="@drawable/listdivider" 
      android:dividerHeight="19dp" 

      /> 
</LinearLayout> 
</LinearLayout> 

所以你設置兩個變量爲列表視圖,並基於「的Xlist」將「ylist」之前出現在XML 。但是對於我的代碼,如果滿足某個條件,我想切換此視圖的順序。那麼我將如何去切換訂單,以便如果滿足某個條件,「ylist」將出現在「xlist」之上?

+0

我猜你的意思是編程,右? – m0skit0

+0

修正得如此之多 Xadapter = xadapter; Yadapter = yadapter; xlist =(ListView)findViewById(R.id.xlist); ylist =(ListView)findViewById(R.id.ylist); – user1823974

+0

好吧,你不需要XML標記,既不是OOP標記,因爲你的問題與OOP本身無關:) – m0skit0

回答

3

一個簡單的方法是將每個視圖放在自己的xml文件中。 然後在運行時將它們以所需順序附加到linearLayout。

說你的佈局文件:main.xml中 ,你有list_a.xml,list_b.xml和textview.xml

您的活動:

@Override 

public void onCreate(Bundle bundle) { 
Super.onCreate(bundle); 
setContentView(R.layout.main); 
LinearLayout layout = (LinearLayout) findViewById(R.id.layout); 
LayoutInflater inflater = getLayoutInflater(); 
if (condition) { 
inflater.inflate(R.layout.list_a, layout); 
inflater.inflate(R.layout.textview, layout); 
inflater.inflate(R.layout.list_b, layout); 


} else { 
    inflater.inflate(R.layout.list_b, layout); 
inflater.inflate(R.layout.textview, layout); 
inflater.inflate(R.layout.list_a, layout); 
} 
} 
+0

是的,這絕對是一個更好的解決方案。好決定。 – m0skit0

+0

好的,很好的解決方案,但我尋求更簡單的解決方案。就像重疊佈局一樣,您可以爲每個佈局指定一個「z」值,這將很容易在代碼中進行更改,因爲您可以將2佈局z值從0更改爲1.有沒有類似的列表佈局? – user1823974

+0

您可以使用兩個不同的xml文件進行佈局,並選擇合適的條件。我不確定你爲什麼首先做這件事 –

0

決不這樣做,但這應該工作:

public void switchViewOrder(final ViewGroup parent, final int view1Id, final int view2Id) { 

    View view1 = null; 
    int view1pos = -1; 
    View view2 = null; 
    int view2pos = -1; 

    int count = parent.getChildCount(); 
    for(int i = 0; i < count; i++) { 
     View cur = parent.getChildAt(i); 
     if (view1Id == cur.getId()) { 
      view1 = cur; 
     } else if (view2Id == cur.getId()) { 
      view2 = cur; 
     } 
    } 

    parent.removeViewAt(view1pos); 
    parent.removeViewAt(view2pos); 

    parent.addView(view1, view2pos); 
    parent.addView(view2, view1pos); 
} 

我留給你添加適當的零和的樣檢查。