在运行时将按钮添加到布局

发布于 2024-11-26 10:49:17 字数 1609 浏览 0 评论 0原文

我正在开发一个 Android 项目,我需要在运行时向布局添加按钮。所以,这是布局 xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/layout">

</LinearLayout>

这是 Activity:

import android.app.Activity;
import android.widget.*;
import android.widget.TableLayout.LayoutParams;
import android.os.Bundle;

public class PluginsActivity extends Activity{

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plugins);

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        Button butt = new Button(this);
        Button butt2 = new Button(this);
        butt.setText("lol");
        butt2.setText("lol2");
        butt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        butt2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        layout.addView(butt);
        layout.addView(butt2);

    }

}

现在,问题是当我启动 Activity 时,按钮 1“覆盖”按钮 2。事实上,如果我使用 LayoutParams.WRAP_CONTENT、LayoutParams.WRAP_CONTENT) 两者显示按钮。有没有办法在 LinearLayout 上添加“新行”?屏幕应如下所示:


|*******Button1 **********|


|*******按钮2*** *******|


谢谢。

I'm working on an Android project and I need to add buttons to a Layout on runtime. So, this is the layout xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/layout">

</LinearLayout>

And this is the Activity:

import android.app.Activity;
import android.widget.*;
import android.widget.TableLayout.LayoutParams;
import android.os.Bundle;

public class PluginsActivity extends Activity{

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plugins);

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
        Button butt = new Button(this);
        Button butt2 = new Button(this);
        butt.setText("lol");
        butt2.setText("lol2");
        butt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        butt2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        layout.addView(butt);
        layout.addView(butt2);

    }

}

Now, the problem is that when i start the activity, the button 1 "covers" the button 2. In fact if I use LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT) both the buttons are shown. Is there a way to add a "new line" on the LinearLayout? The screen should be like this:


|*******Button1**********|


|*******Button2**********|


Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过期以后 2024-12-03 10:49:17

在布局文件中使 LinearLayout 的方向垂直

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

,然后

butt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
butt2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

Make the oritentation vertical of your LinearLayout in the layout file

< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

and

butt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
butt2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
拥抱我好吗 2024-12-03 10:49:17

Activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

</LinearLayout>

// 提及 android:orientation="vertical"

....这是主要问题...使其垂直并且可以工作

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

</LinearLayout>

// mention the android:orientation="vertical"

.... this was the main problem....make it vertical and will work

故乡的云 2024-12-03 10:49:17
Activity_Main.java
____________________________________________________________________________

package com.example.dynamic;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout layout = (LinearLayout)findViewById(R.id.layout);

        Button b1 = new Button(this);
        b1.setText("A");

        Button b2 = new Button(this);
        b2.setText("B");

        Button b3 = new Button(this);
        b3.setText("C");

        Button b4 = new Button(this);
        b4.setText("D");

        Button b5 = new Button(this);
        b5.setText("E");

        Button b6 = new Button(this);
        b6.setText("F");

        layout.addView(b1);
        layout.addView(b2);
        layout.addView(b3);
        layout.addView(b4);
        layout.addView(b5);
        layout.addView(b6);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
Activity_Main.java
____________________________________________________________________________

package com.example.dynamic;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout layout = (LinearLayout)findViewById(R.id.layout);

        Button b1 = new Button(this);
        b1.setText("A");

        Button b2 = new Button(this);
        b2.setText("B");

        Button b3 = new Button(this);
        b3.setText("C");

        Button b4 = new Button(this);
        b4.setText("D");

        Button b5 = new Button(this);
        b5.setText("E");

        Button b6 = new Button(this);
        b6.setText("F");

        layout.addView(b1);
        layout.addView(b2);
        layout.addView(b3);
        layout.addView(b4);
        layout.addView(b5);
        layout.addView(b6);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文