想象一下這樣的層次: 案例1的Android,RelativeLayout.BELOW不是裏面滾動型
RelativeLayout (screen size)
|
-> RelativeLayout (centered)
| |->TextView
|
|
-> buttonLayout (below the former RelativeLayout)
|->Button
有了這個,我得到預期的結果,在屏幕的中間和下方的按鈕一個TextView。
但是,如果我添加滾動:
Case 2
ScrollView
|
-> RelativeLayout (screen size)
|
-> RelativeLayout (centered)
| |->TextView
|
|
-> buttonLayout (below the former RelativeLayout)
|->Button
然後一切看起來相同,但按鈕忽略「低於規則」,並在屏幕頂部顯示的差不多。我只是不明白爲什麼。任何幫助?
感謝您的幫助。 格雷格
代碼: //準備情況2
public View createOwnLayout()
{
ScrollView scrollView = new ScrollView(this);//Comment for case 1
RelativeLayout 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
}
當我宣揚XML佈局時,我傾向於討厭,所以請耐心等待。是否有任何理由不是XML佈局?出於各種原因,調試佈局問題變得更容易上百萬倍。最大的原因是你可以實時編輯佈局。我知道我甚至不會花時間閱讀那些代碼,因爲程序化佈局是一個痛苦的屁股。 – 2011-04-28 12:27:09
@Mike同意。爲什麼要在100行Java代碼中進行佈局?非常不可讀。 – 2012-05-23 01:12:49