Android,RelativeLayout.BELOW在ScrollView中不起作用

时间:2011-04-28 11:55:37

标签: android scrollview relativelayout

想象一下这个层次结构: 案例1

RelativeLayout (screen size)
        |
        -> RelativeLayout (centered)
        |   |->TextView
        |
        |
        -> buttonLayout (below the former RelativeLayout)
            |->Button

有了这个,我得到了预期的结果,屏幕中间和按钮下方的文本视图。

但如果我添加一个卷轴:

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 个答案:

答案 0 :(得分:5)

在创建scrollView后尝试添加以下内容:

scrollView.setFillViewport( true );

我同意Mike的意见,即使用XML布局会使维护更容易,并且您在当前版本的ADT中具有可视化编辑器的优势。