Button 和 EditTextbox 以及布局问题

发布于 2024-12-12 07:42:14 字数 2064 浏览 0 评论 0原文

这是我的代码。因此,当单击按钮时,每次单击按钮时都会显示两个编辑文本框。他们来来往往,我希望他们并肩而行。 我还想知道我可以将线性布局更改为绝对布局,因为我希望按钮位置位于页面中间。另外,我希望当我单击按钮时,编辑文本框应该位于顶部而不是底部。 希望如此,我把这个问题说清楚了。

java代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

    public class PlusbuttonActivity extends Activity {
        /** Called when the activity is first created. */
        LinearLayout root;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

        Button mButton = (Button) findViewById(R.id.button);
        root = (LinearLayout) findViewById(R.id.linearLayout);
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText t = new EditText(PlusbuttonActivity.this);
                EditText a = new EditText(PlusbuttonActivity.this);

                t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                a.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                addViewToRoot(t);
                addViewToRoot(a);
            } 
        });

        }

        private void addViewToRoot(View v){
          root.addView(v);
        }

    }

xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
    <Button android:layout_width="wrap_content" android:text="+" android:id="@+id/button" android:layout_height="wrap_content"></Button>
</LinearLayout>

This is my code. So when button is clicked, I have two edit text boxes that show up everytime, Button is clicked. They are coming up and down, instead I want them to be side by side.
Also I wanted to know that can I change the linear layout to absolute, since I want the button position to be at the middle of the page. Also, I want when I click the button edit text boxes should go to the top not at the bottom.
Hope so, I made this questions clear.

java code :

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

    public class PlusbuttonActivity extends Activity {
        /** Called when the activity is first created. */
        LinearLayout root;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

        Button mButton = (Button) findViewById(R.id.button);
        root = (LinearLayout) findViewById(R.id.linearLayout);
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText t = new EditText(PlusbuttonActivity.this);
                EditText a = new EditText(PlusbuttonActivity.this);

                t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                a.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                addViewToRoot(t);
                addViewToRoot(a);
            } 
        });

        }

        private void addViewToRoot(View v){
          root.addView(v);
        }

    }

xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
    <Button android:layout_width="wrap_content" android:text="+" android:id="@+id/button" android:layout_height="wrap_content"></Button>
</LinearLayout>

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

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

发布评论

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

评论(1

梦回旧景 2024-12-19 07:42:14

为了使它们并排改变线性布局方向=“水平”,但对我来说,听起来你宁愿使用RelativeLayout。在遇到困难之前先查一下它并尝试使用它 http:// developer.android.com/resources/tutorials/views/hello-relativelayout.html

To make them side by side change linearlayout orientation="horizontal" but to me it sounds like you'd rather play around with RelativeLayout. Look it up before you get stuck and try to play with it http://developer.android.com/resources/tutorials/views/hello-relativelayout.html.

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