【问题标题】:How do I change the order of the textviews and buttons in a linerLayout or RelativeLayout?如何更改 linearLayout 与 RelativeLayout 中文本视图和按钮的顺序?
【发布时间】:2013-04-02 04:03:46
【问题描述】:

我对 stackoverflow 完全陌生,并且只有几周的 android 移动开发经验。

非常感谢 stackoverflow,我 90% 的 Google 搜索都以“Android eclipse”开头。

现在开始追逐。

我有一个带有按钮和两个 textView 元素的线性布局。

是否可以动态:

  1. 更改订单?
  2. 将其中一些移动到另一个 linearLayout 或 relativeLayout?

【问题讨论】:

标签: android eclipse


【解决方案1】:

动态是指在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);

【讨论】:

  • 非常感谢几行代码让我免于大量搜索!
【解决方案2】:
  1. 是的,即使在 XML 中添加了 Views,也可以动态更改视图的顺序。
  2. 是的,可以将子视图从一种布局移动到另一种布局。

查看此示例代码:

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>

【讨论】:

  • 我希望这是对您的回答和代码摘录表示感谢的正确地方!
  • 你可以给我的答案,一个接受和一个upvote,这也是表达感谢的正确方式;)
  • Darwind,感谢您的代码。但是,它会出现错误,而且因为我对此很陌生,所以我不知道该怎么处理它们!这是我遇到的列表://我使用 Alt-Control O 来获取以下导入... import android.app.Activity;导入 android.content.DialogInterface.OnClickListener;导入android.os.Bundle;导入android.view.View;导入android.widget.Button;导入 android.widget.CheckBox;导入 android.widget.LinearLayout;下一条评论中的更多内容(我怎样才能写出超出这个有限空间的内容!)
  • // 下面的行给出以下错误: View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数 (new DialogInterface.OnClickListener(){}) button1.setOnClickListener(new OnClickListener() { // 下面的行给出了以下错误: 类型为 new DialogInterface.OnClickListener(){} 的方法 onClick(View) 必须覆盖或实现超类型方法 public void onClick(View arg0) { 最后一个屏幕随后出现两条错误消息...
  • Forgot to write @Darwind --- // 下面的行给出了以下错误: View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数 (new DialogInterface.OnClickListener() {}) button3.setOnClickListener(new OnClickListener() { ... // 下面的行给出以下错误: new DialogInterface.OnClickListener(){} 类型的方法 onClick(View) 必须覆盖或实现超类型方法 public void onClick (查看 v){
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-31
  • 2012-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多