Android 如何更改linerLayout或RelativeLayout中文本视图和按钮的顺序?

Android 如何更改linerLayout或RelativeLayout中文本视图和按钮的顺序?,android,eclipse,Android,Eclipse,我是stackoverflow的新手,在android手机开发方面只有几周的时间 非常感谢stackoverflow,我90%的谷歌搜索都是从Android eclipse开始的 现在开始追逐 我有一个线性布局,比如说一个按钮和两个textView元素 是否可以动态地: 改变他们的顺序? 将其中一些移动到另一个线性布局或相对布局? 您指的是java代码中的动态 如果是,那么这些视图也应该动态添加。 然后,要更改它们的顺序,您可以从布局中删除它们,然后按照您希望的顺序再次添加它们。 如果您的意思是

我是stackoverflow的新手,在android手机开发方面只有几周的时间

非常感谢stackoverflow,我90%的谷歌搜索都是从Android eclipse开始的

现在开始追逐

我有一个线性布局,比如说一个按钮和两个textView元素

是否可以动态地:

改变他们的顺序? 将其中一些移动到另一个线性布局或相对布局?
您指的是java代码中的动态

如果是,那么这些视图也应该动态添加。 然后,要更改它们的顺序,您可以从布局中删除它们,然后按照您希望的顺序再次添加它们。 如果您的意思是在XML文件中,那么您所需要做的就是更改它们在XML文件中的顺序

要将视图添加到布局,您应该:

一,。按id查找布局:

LinearLayout llviews = (LinearLayout) findViewById(R.id.your_linear_layout_id);
二,。然后,可以将视图添加到此布局:

TextView tvText = new TextView();
llviews.addView(tvText); 
三,。并删除一个视图:

llviews.removeView(tvText);
是的,即使视图是以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);
    }
}
==活动\u 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>

我希望这是说谢谢你的答案和代码摘录的正确地方!你可以给我一个答案,一个接受,一个向上投票,这也是表达感谢的正确方式;达文德,谢谢你的密码。但是,它会出现错误,因为我对这一点太陌生了,我不知道该怎么办!下面是我遇到的问题的列表://我使用Alt Control O获得以下导入。。。导入android.app.Activity;导入android.content.DialogInterface.OnClickListener;导入android.os.Bundle;导入android.view.view;导入android.widget.Button;导入android.widget.CheckBox;导入android.widget.LinearLayout;下一篇评论中有更多内容我如何才能写得比这有限的篇幅更多!//下一行给出以下错误:类型视图中的方法setOnClickListenerView.OnClickListener不适用于参数new DialogInterface。OnClickListener{}按钮1.setOnClickListenernew OnClickListener{//下一行给出以下错误:类型new DialogInterface.OnClickListener{}的方法onClickView必须重写或实现超类型方法public void onClickView arg0{后面还有一个屏幕显示最后两条错误消息…忘记写入@Darwind-//下面的行给出了以下错误:类型视图中的方法setOnClickListenerView.OnClickListener不适用于参数new DialogInterface.OnClickListener{}button3.setOnClickListenernew OnClickListener{…//下面的一行给出了以下错误:类型为new DialogInterface的方法onClickView。OnClickListener{}必须重写或实现一个超类型方法public void onClickView v{非常感谢为我提供了几行代码,这些代码节省了大量搜索时间!可能重复了