问题在一个相对布局添加文本框dynamiclly [英] Problem adding textboxes dynamiclly in a relative layout

查看:313
本文介绍了问题在一个相对布局添加文本框dynamiclly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法动态地添加一个文本框,以我的看法。更具体地,文本框加器的工作原理,但按钮我试图移动以下事实并非如此。原来的观点是在下面的超链接的第一张照片。

I am having trouble dynamically adding a text box to my view. More specifically, the text-box-adder works, but the button I am trying to move below it does not. The original view is the first picture in the below hyperlink.

+/-按钮是pressed后,应该添加一个文本框第二个文本框和按钮决定之间,然后移动+/-按钮所以它是未来新的框。相反,第二张照片发生了:

After the +/- button is pressed, it should add a text box between the second text box and the decide button, and then move the +/- button down so it is next to the new box. Instead, the second picture happens:

http://i.stack.imgur.com/mzBL3.png

我的code是这样的:

My code looks like this:

EditText textBox2 = (EditText) findViewById(R.id.box2);

            RelativeLayout rel = (RelativeLayout) findViewById(R.id.mainlayout);

            Context context = getApplicationContext();
            EditText newText = new EditText(context);

            newText.setId(numBoxes);
            numBoxes++;

            LayoutParams p = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            p.addRule(RelativeLayout.BELOW, bottomView.getId());
            p.addRule(RelativeLayout.ALIGN_RIGHT, bottomView.getId());
            newText.setWidth(220);
            newText.setHeight(LayoutParams.WRAP_CONTENT);
            newText.setLayoutParams(p);
            rel.addView(newText);
            bottomView = newText;
            ((TextView) bottomView).setText((CharSequence)bottomView.getTag());

            LayoutParams b = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            Button goButton = (Button)findViewById(R.id.decide);
            b = (LayoutParams) goButton.getLayoutParams();
            b.addRule(RelativeLayout.BELOW, bottomView.getId());
            goButton.setLayoutParams(b);

            Button addButton = (Button)findViewById(R.id.addsub);
            b = (LayoutParams) addButton.getLayoutParams();
            b.addRule(RelativeLayout.RIGHT_OF, textBox2.getId());
            b.addRule(RelativeLayout.ALIGN_TOP, bottomView.getId());
            addButton.setLayoutParams(b);

Numboxes只是一个计数器,以便我可以跟踪的箱子,因此他们的名字,和bottomView仅仅是最底层的文本框。

Numboxes is just a counter so I can keep track of the boxes and name them accordingly, and bottomView is just the bottom-most text box.

我已经找遍了Android开发的网页,并试图创造新的布局PARAMS。我也试图与一个文本框代替决定按钮,府发生同样的问题。请帮助。

I have searched all over the android development pages, and tried creating the new layout params. I also tried replacing the decide button with a text box, bu the same problem occurred. Please help.

推荐答案

对于那些关心,我是这个开发团队的一部分,我们得到了我们的问题解决了。

For those that care, I'm part of this development team and we got our problem solved.

这是问题的概念。

LayoutParams p = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

在我们的布局中,我们使用的是相对的布局,因此该行应该是:

In our layout, we're using a relative layout, thus the line should be:

 RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

分配不兼容的布局参数会导致问题。

Assigning incompatible layout parameters causes problems.

这篇关于问题在一个相对布局添加文本框dynamiclly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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