2013-02-10 50 views
0

我有一個水平線性佈局作爲我的佈局文件中的根元素。線性佈局包含兩個相對佈局,每個佈局都有幾個子元素。我怎樣才能交換這兩個相對佈局的位置?在運行時重新排序android線性佈局?

我試圖

ViewGroup vg = (ViewGroup)findViewById(R.id.horizontalLayout); 
RelativeLayout rl1 = (RelativeLayout)findViewById(R.id.panelLayout); 
RelativeLayout rl2 = (RelativeLayout)findViewById(R.id.boardLayout); 
vg.removeAllViews(); 
vg.addView(rl2); 
vg.addView(rl1); 

我還試圖

vg.removeView(rl2); 
vg.addView(rl2, 0); 

兩個結果在第二相對佈局(RL2)佔用的整個寬度。如果需要,我可以發佈我的xml文件,但它很長。此外,兩個相對佈局的height = match_parent和width = wrap_content。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/horizontalLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<RelativeLayout 
    android:id="@+id/panelLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="@string/next" /> 

    <com.mydomain.tetrisv2.PreviewGridView 
     android:id="@+id/previewGridView1" 
     android:layout_width="100dp" 
     android:layout_height="150dp" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" /> 

    <TextView 
     android:id="@+id/txtScore" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/previewGridView1" 
     android:text="Score: 0" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="@string/pausestr" 
     android:onClick="btnPause_Click" /> 

    <TextView 
     android:id="@+id/lblPaused" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/txtScore" 
     android:layout_marginTop="20dp" 
     android:text="@string/pausedstr" 
     android:visibility="gone" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button1" 
     android:layout_alignRight="@+id/previewGridView1" 
     android:layout_marginBottom="41dp" 
     android:text="@string/savestr" 
     android:onClick="btnSave_Click" /> 

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/boardLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" > 

    <com.mydomain.tetrisv2.GridView 
     android:id="@+id/gridView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <ImageButton 
     android:id="@+id/btnDrop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:src="@drawable/downarrow" 
     android:background="@null" 
     android:onClick="btnDrop_Click" /> 

    <ImageButton 
     android:id="@+id/btnLeft" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:src="@drawable/leftarrow" 
     android:background="@null" 
     android:onClick="btnLeft_Click" /> 

    <ImageButton 
     android:id="@+id/btnRight" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:src="@drawable/rightarrow" 
     android:background="@null" 
     android:onClick="btnRight_Click" /> 

</RelativeLayout> 

</LinearLayout> 
+0

使用層次結構視圖來幫助您確定您的方法出錯的位置。沒有佈局文件,任何人都很難幫助你。你的代碼看起來很好。 – CommonsWare 2013-02-10 23:36:06

+0

以爲我描述了我的佈局文件,至少是最重要的部分,但我想你是正確的病後文件。 – user1938939 2013-02-10 23:40:13

回答

0

嘗試切換到的addView()一個味道,需要LayoutParams作爲第二值,並提供您當前在XML設定值(「HEIGHT = match_parent和寬度= WRAP_CONTENT」)相匹配的LinearLayout.LayoutParams

+0

剛剛嘗試過相同的結果。 – user1938939 2013-02-10 23:50:31

+0

@ user1938939:嗯......我回到層次結構視圖的建議,因爲它應該幫助識別沒有得到正確的'LayoutParams'值。 – CommonsWare 2013-02-10 23:52:46

+0

現在看看它。非常有趣的不知道這個工具存在,我很新的android – user1938939 2013-02-11 18:45:27