我要如何改变一个linerLayout或RelativeLayout的的textviews和按钮的顺序? [英] How do I change the order of the textviews and buttons in a linerLayout or RelativeLayout?

查看:149
本文介绍了我要如何改变一个linerLayout或RelativeLayout的的textviews和按钮的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全新的StackOverflow,只有用Android手机开发了几个星期。

有关计算器,这是我的土地上的,与机器人蚀开始我的谷歌搜索90%非常感谢。

现在正题。

我有一个的LinearLayout有说按钮和两个TextView的元素。

是否有可能动态:


  1. 改变它们的顺序?

  2. 将他们中的一些其他的LinearLayout或RelativeLayout的?


解决方案

  1. 是的,它可以动态改变视图的顺序,即使查看在XML中添加。

  2. 是有可能从一种布局走动子视图到另一个布局。

看看这个例子code:

 公共类MainActivity延伸活动{
    私人按钮按钮1,按钮2,按钮3;
    私人复选框checkBox1;
    私人的LinearLayout LL1,LL2;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        LL1 =(的LinearLayout)findViewById(R.id.ll1);
        LL2 =(的LinearLayout)findViewById(R.id.ll2);        BUTTON1 =(按钮)findViewById(R.id.button1);
        button1.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(查看为arg0){
                //四处移动的看法。
                浮button2x = button2.getX();
                浮button2y = button2.getY();                浮checkbox1x = checkBox1.getX();
                浮checkbox1y = checkBox1.getY();                button2.setX(checkbox1x);
                button2.setY(checkbox1y);                checkBox1.setX(button2x);
                checkBox1.setY(button2y);
            }
        });        按钮3 =(按钮)findViewById(R.id.button3);
        button3.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                //从一种布局移动视图到另一个。
                ll1.removeView(按钮2);
                ll2.addView(按钮2);
            }
        });        按钮2 =(按钮)findViewById(R.id.button2);        checkBox1 =(复选框)findViewById(R.id.checkBox1);
    }
}

=== activity_main.xml中===

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直机器人:ID =@ + ID / LL1>    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginLeft =42dp
        机器人:layout_marginTop =22dp
        机器人:文字=按钮/>    <按钮
        机器人:ID =@ + ID /按钮2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=Button2的/>    <复选框
        机器人:ID =@ + ID / checkBox1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginTop =99dp
        机器人:文字=复选框/>    <的LinearLayout机器人:ID =@ + ID / [112
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT>        <按钮
            机器人:ID =@ + ID /按钮3
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=按钮3/>    < / LinearLayout中>< / LinearLayout中>

I am totally new to stackoverflow, and only a couple of weeks with android mobile development.

A big thanks for stackoverflow, which is where I land on 90% of my google searches that begin with "Android eclipse".

Now to the chase.

I have a linearLayout with say a button and two textView elements.

Is it possible to dynamically:

  1. Change their order?
  2. Move some of them to another linearLayout or relativeLayout?

解决方案

  1. Yes it is possible to change the order of views dynamically, even though the Views are added in XML.
  2. Yes it's possible to move sub-views around from one layout to another layout.

Check out this example code:

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>

这篇关于我要如何改变一个linerLayout或RelativeLayout的的textviews和按钮的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆