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

标签 android scrollview android-relativelayout

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

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

 }

最佳答案

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

scrollView.setFillViewport( true );

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

关于Android,RelativeLayout.BELOW 在 ScrollView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5818187/

相关文章:

android - Android 操作栏中的自定义大 Logo

android - 编译 Android 内核的问题 + 使用 LiME 进行交叉编译

android - android :softinputmode ="adjustpan" enabled时scrollview不会滚动

android - 如何在 Android Studio 中从默认 ConstraintLayout 切换到 RelativeLayout

android - 如何在代码中而不是在 xml 中设置 RelativeLayout 布局参数?

android - Notification onClick Intent 向 Activity 发送错误数据

android - SQLite 数据库助手类

java - 垂直拆分 Android 布局向两侧添加重力以使对象居中

android - 使用 Horizo​​ntalScrollview 选项卡主机滚动并居中

iphone - 更新 ScrollView 的内容