2011-04-28 58 views
1
工作

想象一下这样的层次: 案例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 

} 
+1

当我宣扬XML布局时,我倾向于讨厌,所以请耐心等待。是否有任何理由不是XML布局?出于各种原因,调试布局问题变得更容易上百万倍。最大的原因是你可以实时编辑布局。我知道我甚至不会花时间阅读那些代码,因为程序化布局是一个痛苦的屁股。 – 2011-04-28 12:27:09

+0

@Mike同意。为什么要在100行Java代码中进行布局?非常不可读。 – 2012-05-23 01:12:49

回答

5

尝试添加下列创建刚过滚动视图:

scrollView.setFillViewport(true); 

我愿意使用XML与Mike的评论表示赞同布局会使维护更容易,而且在当前版本的ADT中您拥有可视化编辑器的优势。

+0

感谢马克它的工作。 – Greg 2011-04-28 12:47:42

+0

很高兴听到它。请将此标记为“接受的答案”,详细信息请参见http://stackoverflow.com/faq#howtoask。这样其他人会知道修复工作。 – 2011-04-28 12:52:38

+1

感谢马克它的工作。我试图用XML来做,但我改变了主意。我的经验是使用代码比使用XML或可视化编辑器更好地制作图形用户界面,为复杂的界面提供更多的工作但更少的问题。 – Greg 2011-04-28 12:53:14