我对stackoverflow完全陌生,并且只有几周的android移动开发。
非常感谢 stackoverflow,我 90% 的谷歌搜索都以“Android eclipse”开头。
现在开始追逐。
我有一个带有按钮和两个 textView 元素的线性布局。
是否可以动态:
- 改变他们的顺序?
- 将其中一些移动到另一个 linearLayout 或 relativeLayout?
动态是指在Java代码中吗?
如果是,那么这些视图也应该动态添加。然后要更改它们的顺序,您可以将它们从布局中删除,然后按照您想要的顺序再次添加它们。如果您的意思是在您的 XML 文件中,那么您需要做的就是更改它们在 XML 文件中的顺序。
要将视图添加到布局中,您应该:
1.通过id查找布局:
LinearLayout llviews = (LinearLayout) findViewById(R.id.your_linear_layout_id);
2.然后,您可以向此布局添加视图:
TextView tvText = new TextView();
llviews.addView(tvText); 
3.删除视图:
llviews.removeView(tvText);
Views是在 XML 中添加的,也可以动态更改视图的顺序。查看此示例代码:
public class MainActivity extends Activity {
    private Button button1, button2, button3;
    private CheckBox checkBox1;
    private LinearLayout ll1, ll2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll1 = (LinearLayout) findViewById(R.id.ll1);
        ll2 = (LinearLayout) findViewById(R.id.ll2);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // Move views around.
                float button2x = button2.getX();
                float button2y = button2.getY();
                float checkbox1x = checkBox1.getX();
                float checkbox1y = checkBox1.getY();
                button2.setX(checkbox1x);
                button2.setY(checkbox1y);
                checkBox1.setX(button2x);
                checkBox1.setY(button2y);
            }
        });
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Move a view from one layout to another.
                ll1.removeView(button2);
                ll2.addView(button2);
            }
        });
        button2 = (Button) findViewById(R.id.button2);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
    }
}
=== activity_main.xml ===
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:id="@+id/ll1">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="22dp"
        android:text="Button" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2" />
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="99dp"
        android:text="CheckBox" />
    <LinearLayout android:id="@+id/ll2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3" />
    </LinearLayout>
</LinearLayout>