レイアウトファイルのルート要素として水平線形レイアウトがあります。線形レイアウトには、それぞれにいくつかの子要素を持つ2つの相対レイアウトが含まれています。これら2つの相対レイアウトの位置を入れ替えるにはどうすればよいですか?
私は試した
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);
どちらも、幅全体を占める2番目の相対レイアウト(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>