Android, RelativeLayout.BELOW not working inside ScrollView
Imagine this hierarchy: Case 1
RelativeLayout (screen size)
|
-> RelativeLayout (centered)
| |->TextView
|
|
-> buttonLayout (below the former RelativeLayout)
|->Button
With this I get the expected result, a textview in the middle of the screen and just below a button.
But if I add a scroll:
Case 2
ScrollView
|
-> RelativeLayout (screen size)
|
-> RelativeLayout (centered)
| |->TextView
|
|
-> buttonLayout (below the former RelativeLayout)
|->Button
Then the everything looks the same but the button ignores the 'below rule' and is displayed almost at the top of the screen. I just can not understand why. Any help?
Thanks for helping. Greg
The code: //Ready for case 2
public View createOwnLayout()
{
ScrollView scrollView = new ScrollView(this);//Comment for case 1
Relative开发者_运维百科Layout parentView = new RelativeLayout(this);
parentView.setBackgroundColor(0xff990000);
//--------------------------------- LINEAR LLAYOUT ---------------------------------//
LinearLayout centerLL = new LinearLayout(this);
centerLL.setId(CommonOpsAndValues.CONNECTION_ACTIVITY_IMAGE_LINEAR_LAYOUT);
centerLL.setOrientation(LinearLayout.VERTICAL);
centerLL.setBackgroundResource(R.drawable.rainbow);
centerLL.setBackgroundColor(0xff555599);
RelativeLayout.LayoutParams centerLLLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
centerLLLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
centerLLLP.addRule(RelativeLayout.CENTER_IN_PARENT);
parentView.addView(centerLL,centerLLLP);
TextView serverLabel = new TextView(this);
serverLabel.setText("HELLO");
serverLabel.setTextColor(0xffffffff);
serverLabel.setTextSize(LABEL_TEXT_SIZE);
serverLabel.setPadding(INPUT_TEXTBOX_TEXT_PADDING, 0,0,0);
LinearLayout.LayoutParams serverLabelLLP = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
serverLabelLLP.gravity = Gravity.LEFT;
centerLL.addView(serverLabel,serverLabelLLP);
//---------------------------- LINEAR LLAYOUT ---------------------------/
//---------------------------- BUTTON LAYOUT---------------------------/
LinearLayout buttonLayout = new LinearLayout(this);
buttonLayout.setGravity(Gravity.CENTER);
buttonLayout.setBackgroundColor(0x00000000);
Button button = new Button(this);
button.setText("DO");
button.setClickable(true);
LinearLayout.LayoutParams buttonLP = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
buttonLayout.addView(button,buttonLP );
//---------------------------- BUTTON LAYOUT ---------------------------/
RelativeLayout.LayoutParams buttonLayoutLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonLayoutLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
buttonLayoutLP.addRule(RelativeLayout.BELOW,centerLL.getId());
parentView.addView(buttonLayout,buttonLayoutLP);
scrollView.addView(parentView,new FrameLayout.LayoutParams(AppManager.g_ScreenWidth,AppManager.g_ScreenHeight));//Comment for case 1
scrollView.setFillViewport(false);//Comment for case 1
//return parentView; //Uncomment for case 1
return scrollView;//Comment for case 1
}
Try adding the following just after you create scrollView:
scrollView.setFillViewport( true );
I would agree with Mike's comment that using an XML layout would make it much easier to maintain, and you have the advantage of a visual editor in the current version of ADT.
精彩评论