【问题标题】:Reorder android linear layout at runtime?在运行时重新排序android线性布局?
【发布时间】:2013-02-11 07:43:12
【问题描述】:

我的布局文件中有一个水平线性布局作为我的根元素。线性布局包含两个相对布局,每个布局都有几个子元素。如何交换这两个相对布局的位置?

我试过了

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>

【问题讨论】:

  • 使用层次结构视图来帮助您确定您的方法哪里出了问题。没有布局文件,任何人都很难帮助您。你的代码看起来不错。
  • 我认为我很好地描述了我的布局文件,至少重要的部分,但我想你是对的,发布文件。

标签: android layout


【解决方案1】:

尝试切换到将LayoutParams 作为第二个值的addView() 风格,并提供与您当前在XML 中设置的值匹配的LinearLayout.LayoutParams(“height=match_parent 和 width=wrap_content” )。

【讨论】:

  • @user1938939: 嗯……好吧,我回到层次视图建议,因为它应该有助于确定没有得到正确的LayoutParams 值。
  • 现在正在调查。非常有趣,不知道这个工具的存在,我对 android 很陌生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多