I am having trouble dynamically adding a text box to my view. More specifically,开发者_JAVA百科 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.
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
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 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.
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.
Here is the problem concept.
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.
精彩评论