1

在我的 android 应用程序中,我需要如下所示的布局

在此处输入图像描述

内部使用相对布局作为父布局我使用4个相对布局来排列内容-标题布局1,标题布局2,内容布局(objRLScrollView - 滚动视图在此布局内)和页脚布局

我使用下面的代码来创建上面的布局

    public class Login1 extends Activity {




     @Override
        public void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

           this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
           this.requestWindowFeature(Window.FEATURE_NO_TITLE);

            RelativeLayout objRLBody=new RelativeLayout(this);
            objRLBody.setId(1001);
            RelativeLayout.LayoutParams objLLBodyParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
            objRLBody.setBackgroundColor(Color.parseColor("#d8e4f3"));
            objRLBody.setLayoutParams(objLLBodyParams); 


            /* header portion starts here*/

            RelativeLayout objRLActionBar=new RelativeLayout(this);
            objRLActionBar.setId(1002);
            RelativeLayout.LayoutParams objRLActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,50);
            objRLActionBarParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
            objRLActionBar.setLayoutParams(objRLActionBarParams);
            objRLActionBar.setBackgroundColor(Color.parseColor("#2e4862"));



            objRLBody.addView(objRLActionBar);


            RelativeLayout objRLSelectedActionBar=new RelativeLayout(this);
            objRLSelectedActionBar.setId(1003);
            RelativeLayout.LayoutParams objRLSelectedActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
            objRLSelectedActionBarParams.addRule(RelativeLayout.BELOW,objRLActionBar.getId()); 
            objRLSelectedActionBar.setLayoutParams(objRLSelectedActionBarParams);
            objRLSelectedActionBar.setBackgroundColor(Color.WHITE);
            objRLSelectedActionBar.setGravity(Gravity.CENTER|Gravity.LEFT);


            TextView objTVSelectedAction = new TextView(this); 
            RelativeLayout.LayoutParams objTVSelectedActionParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            objTVSelectedAction.setLayoutParams(objTVSelectedActionParams);
            objTVSelectedAction.setGravity(Gravity.CENTER);
            objTVSelectedAction.setTextColor(Color.BLACK);
            objTVSelectedAction.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);
            objTVSelectedAction.setText("Scroll Test"); 
            objTVSelectedAction.setTypeface(null, Typeface.BOLD);
            objTVSelectedAction.setPadding(4, 0, 4, 0);
            objTVSelectedAction.setId(1004);
            objRLSelectedActionBar.addView(objTVSelectedAction);  


            objRLBody.addView(objRLSelectedActionBar);

            /* header portion ends here*/



            /* Relative layout which hold scroll view*/
            RelativeLayout objRLScrollView = new RelativeLayout(this); 
            objRLScrollView.setId(1005);
            objRLScrollView.setBackgroundColor(Color.parseColor("#d8e4f3"));
            objRLScrollView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
            RelativeLayout.LayoutParams objRLScrollViewParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
            objRLScrollViewParams.addRule(RelativeLayout.BELOW ,objTVSelectedAction.getId());


            ScrollView  objScrollView=new ScrollView(this);
            objScrollView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            objScrollView.setFillViewport(true);



            /* contents */

            RelativeLayout objRLContent = new RelativeLayout(this); 
            objRLContent.setId(1006);
            objRLContent.setBackgroundColor(Color.parseColor("#d8e4f3"));
            objRLContent.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
            RelativeLayout.LayoutParams objRLContentParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
           // objRLContentParams.addRule(RelativeLayout.BELOW ,objTVSelectedAction.getId()); 
            objRLContent.setLayoutParams(objRLContentParams);                


                  /* adding layouts containing Edittext (for testing scroll view) */

                RelativeLayout rl2= addEditText(objRLContent,null,1);
                RelativeLayout rl3= addEditText(objRLContent,rl2,2);
                RelativeLayout rl4= addEditText(objRLContent,rl3,3);
                RelativeLayout rl5= addEditText(objRLContent,rl4,4);
                RelativeLayout rl6= addEditText(objRLContent,rl5,5);
                RelativeLayout rl7= addEditText(objRLContent,rl6,6);
                RelativeLayout rl8= addEditText(objRLContent,rl7,7);
                RelativeLayout rl9= addEditText(objRLContent,rl8,8);
                RelativeLayout rl10= addEditText(objRLContent,rl9,9);
                RelativeLayout rl11= addEditText(objRLContent,rl10,10);
                RelativeLayout rl12= addEditText(objRLContent,rl11,11);
                RelativeLayout rl13= addEditText(objRLContent,rl12,12);






             objScrollView.addView(objRLContent);  // adding contents to scroll view


             objRLScrollView.addView(objScrollView);  // Adding scroll view to a relative layout


             objRLBody.addView(objRLScrollView);  // adding relative layout to body layout



             // adding footer to body

               RelativeLayout objRLFooter=new RelativeLayout(this);
               RelativeLayout.LayoutParams objRLFooterParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,25);
               objRLFooterParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,objRLBody.getId()); 
               objRLFooter.setLayoutParams(objRLFooterParams);
               objRLFooter.setBackgroundColor(Color.parseColor("#2e4862"));
               objRLFooter.setGravity(Gravity.CENTER);
               objRLBody.addView(objRLFooter); 




               this.setContentView(objRLBody); 






      }




     private RelativeLayout addEditText(RelativeLayout objRLContent,RelativeLayout layoutAbove,int i   ) {



            RelativeLayout objRLEditText = new RelativeLayout(this); 
            objRLEditText.setId(1100+i);
            RelativeLayout.LayoutParams objRLEditTextParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            if(layoutAbove!=null)
            objRLEditTextParams.addRule(RelativeLayout.BELOW,layoutAbove.getId());
            objRLEditText.setLayoutParams(objRLEditTextParams);
            objRLEditText.setPadding(8, 2, 8, 2);

             EditText objETData = new EditText(this);
             objETData.setId(1300+i);
             RelativeLayout.LayoutParams objETDataParams=new RelativeLayout.LayoutParams(200,LayoutParams.WRAP_CONTENT);
             objETData.setLayoutParams(objETDataParams);
             objETData.setPadding(8, 0, 8, 0);
             objETData.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);
             objETData.setText(""+i);
             objETData.setSingleLine(true);
             objRLEditText.addView(objETData);           

             objRLContent.addView(objRLEditText);

             return objRLEditText;



     }


}

上面的代码给出了 put 如下所示

在此处输入图像描述

由于滚动视图的存在,标题布局变得不可见。我的代码有什么问题?我应该怎么做才能获得带有固定页眉和页脚的可滚动内容的布局?请帮忙。

4

6 回答 6

6

1- 将 ScrollView 的高度和宽度设置为 match_parent。

2- 将 Scrollview 的顶部和底部填充设置为等于顶部和底部栏的高度。

3-将顶部栏的对齐方式设置为 alignparenttop

4-将底栏的对齐方式设置为 alignparentbottom

注意:如果您想要一个良好的操作栏兼容性库,请考虑使用ActionBarSherlock

于 2012-07-04T10:05:55.463 回答
3

为您的父容器使用 LinearLayout 而不是 RelativeLayout。将 LinearLayout 的方向设置为垂直,并将 ScrollView 的 LayoutWeight 设置为 1。

于 2012-07-04T10:03:45.057 回答
2

尝试followin我已经编辑了你的代码,它工作正常。

public class Scrolling2Activity extends Activity {
    int myid = 1000, pos=-1, etpos=-1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RelativeLayout rlBody = new RelativeLayout(this);
        rlBody.setId(++myid);
        RelativeLayout.LayoutParams bodyParams = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        rlBody.setLayoutParams(bodyParams);
        rlBody.setBackgroundColor(Color.BLACK);

        //      Header-1
        RelativeLayout head1 = new RelativeLayout(this);
        head1.setId(++myid);
        RelativeLayout.LayoutParams head1Params = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, 25);
        head1Params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        head1.setLayoutParams(head1Params);
        head1.setBackgroundColor(Color.BLUE);
        rlBody.addView(head1, ++pos);

        //      Header-2
        RelativeLayout head2 = new RelativeLayout(this);
        head2.setId(++myid);
        RelativeLayout.LayoutParams head2Params = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, 25);
        head2Params.addRule(RelativeLayout.BELOW, head1.getId());
        head2.setLayoutParams(head2Params);
        head2.setBackgroundColor(Color.CYAN);
                //      TextView  in Header-2
        TextView tv1 = new TextView(this);
        tv1.setId(++myid);
        RelativeLayout.LayoutParams tv1Params = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        tv1.setLayoutParams(tv1Params);
        tv1.setGravity(Gravity.CENTER);
        tv1.setTextColor(Color.BLACK);
        tv1.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        tv1.setText("Scroll Test");
        tv1.setTypeface(null, Typeface.BOLD);
        tv1.setPadding(4, 0, 4, 0);
        //tv1.setBackgroundColor(Color.DKGRAY);
        head2.addView(tv1);
        rlBody.addView(head2, ++pos);

        //      RelativeLayout For ScrollView
        RelativeLayout scrollHolder = new RelativeLayout(this);
        scrollHolder.setId(++myid);
        RelativeLayout.LayoutParams scrollHolderParams = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        scrollHolderParams.addRule(RelativeLayout.BELOW, head2.getId());
        scrollHolder.setLayoutParams(scrollHolderParams);
        scrollHolder.setBackgroundColor(Color.DKGRAY);
        scrollHolder.setGravity(Gravity.CENTER_VERTICAL);
            //      ScrollView
        ScrollView scroll = new ScrollView(this);
        scroll.setId(++myid);
        scroll.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        scroll.setBackgroundColor(Color.GREEN);
            //      RelativeLayout
        RelativeLayout etHolder = new RelativeLayout(this);
        etHolder.setId(++myid);
        RelativeLayout.LayoutParams etHolderParams = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        etHolder.setLayoutParams(etHolderParams);
        etHolder.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
        etHolder.setPadding(0, 0, 0, 25);
            //      Creating EditTextes  and Adding to etHolder  AS you have done
        RelativeLayout rl2 = addEditText(etHolder, null, 1);
        RelativeLayout rl3 = addEditText(etHolder, rl2, 2);
        RelativeLayout rl4 = addEditText(etHolder, rl3, 3);
        RelativeLayout rl5 = addEditText(etHolder, rl4, 4);
        RelativeLayout rl6 = addEditText(etHolder, rl5, 5);
        RelativeLayout rl7 = addEditText(etHolder, rl6, 6);
        RelativeLayout rl8 = addEditText(etHolder, rl7, 7);
        RelativeLayout rl9 = addEditText(etHolder, rl8, 8);
        RelativeLayout rl10 = addEditText(etHolder, rl9, 9);
        RelativeLayout rl11 = addEditText(etHolder, rl10, 10);
        RelativeLayout rl12 = addEditText(etHolder, rl11, 11);
        RelativeLayout rl13 = addEditText(etHolder, rl12, 12);
        scroll.addView(etHolder);   //  adding RelativeLayout = etHolder  to  ScrollView = scroll
        scrollHolder.addView(scroll);   //  adding ScrollView = scroll  to  RelativeLayout = scrollHolder
        rlBody.addView(scrollHolder, ++pos);

        //      RelativeLayout  for  Footer
        RelativeLayout footer = new RelativeLayout(this);
        footer.setId(++myid);
        RelativeLayout.LayoutParams footerParams = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, 25);
        footerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        footer.setLayoutParams(footerParams);
        footer.setBackgroundColor(Color.MAGENTA);
        rlBody.addView(footer, ++pos);

        setContentView(rlBody);
    }

        //      Method  as  you  have done
    private RelativeLayout addEditText(RelativeLayout objRLContent,
            RelativeLayout layoutAbove, int i) {

        RelativeLayout objRLEditText = new RelativeLayout(this);
        objRLEditText.setId(1100 + i);
        RelativeLayout.LayoutParams objRLEditTextParams = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if (layoutAbove != null)
            objRLEditTextParams.addRule(RelativeLayout.BELOW,
                    layoutAbove.getId());
        objRLEditText.setLayoutParams(objRLEditTextParams);
        objRLEditText.setPadding(8, 2, 8, 2);

        EditText objETData = new EditText(this);
        objETData.setId(1300 + i);
        RelativeLayout.LayoutParams objETDataParams = new RelativeLayout.LayoutParams(
                200, LayoutParams.WRAP_CONTENT);
        objETData.setLayoutParams(objETDataParams);
        objETData.setPadding(8, 0, 8, 0);
        objETData.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
        objETData.setText("" + i);
        objETData.setSingleLine(true);
        objRLEditText.addView(objETData);

        objRLContent.addView(objRLEditText);

        return objRLEditText;

    }
}
于 2012-07-04T17:03:50.673 回答
1

一旦尝试这个

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  //  setContentView(new BallBounce(this));        

       this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
     //  this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        RelativeLayout objRLBody=new RelativeLayout(this);
        objRLBody.setId(1001);
        RelativeLayout.LayoutParams objLLBodyParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        objRLBody.setBackgroundColor(Color.parseColor("#d8e4f3"));
        objRLBody.setLayoutParams(objLLBodyParams); 

        /* header portion starts here*/

        RelativeLayout objRLActionBar=new RelativeLayout(this);
        objRLActionBar.setId(1002);
        RelativeLayout.LayoutParams objRLActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,50);
        objRLActionBarParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
        objRLActionBar.setLayoutParams(objRLActionBarParams);
        objRLActionBar.setBackgroundColor(Color.parseColor("#2e4862"));

        objRLBody.addView(objRLActionBar);

        RelativeLayout objRLSelectedActionBar=new RelativeLayout(this);
        objRLSelectedActionBar.setId(1003);
        RelativeLayout.LayoutParams objRLSelectedActionBarParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        objRLSelectedActionBarParams.addRule(RelativeLayout.BELOW,objRLActionBar.getId()); 
        objRLSelectedActionBar.setLayoutParams(objRLSelectedActionBarParams);
        objRLSelectedActionBar.setBackgroundColor(Color.WHITE);
        objRLSelectedActionBar.setGravity(Gravity.CENTER|Gravity.LEFT);


        TextView objTVSelectedAction = new TextView(this); 
        RelativeLayout.LayoutParams objTVSelectedActionParams=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        objTVSelectedAction.setLayoutParams(objTVSelectedActionParams);
        objTVSelectedAction.setGravity(Gravity.CENTER);
        objTVSelectedAction.setTextColor(Color.BLACK);
        objTVSelectedAction.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);
        objTVSelectedAction.setText("Scroll Test"); 
        objTVSelectedAction.setTypeface(null, Typeface.BOLD);
        objTVSelectedAction.setPadding(4, 0, 4, 0);
        objTVSelectedAction.setId(1004);
        objRLSelectedActionBar.addView(objTVSelectedAction);  

        objRLBody.addView(objRLSelectedActionBar);

        /* header portion ends here*/

        /* Relative layout which hold scroll view*/
        RelativeLayout objRLScrollView = new RelativeLayout(this); 
        objRLScrollView.setId(1005);
        objRLScrollView.setBackgroundColor(Color.parseColor("#d8e4f3"));
        objRLScrollView.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
        RelativeLayout.LayoutParams objRLScrollViewParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
        objRLScrollViewParams.addRule(RelativeLayout.BELOW ,objTVSelectedAction.getId());


        ScrollView  objScrollView=new ScrollView(this);
        objScrollView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        objScrollView.setFillViewport(true);

        /* contents */

        RelativeLayout objRLContent = new RelativeLayout(this); 
        objRLContent.setId(1006);
        objRLContent.setBackgroundColor(Color.parseColor("#d8e4f3"));
        objRLContent.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
        RelativeLayout.LayoutParams objRLContentParams = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
       // objRLContentParams.addRule(RelativeLayout.BELOW ,objTVSelectedAction.getId()); 
        objRLContent.setLayoutParams(objRLContentParams);                


              /* adding layouts containing Edittext (for testing scroll view) */

            RelativeLayout rl2= addEditText(objRLContent,null,1);
            RelativeLayout rl3= addEditText(objRLContent,rl2,2);
            RelativeLayout rl4= addEditText(objRLContent,rl3,3);
            RelativeLayout rl5= addEditText(objRLContent,rl4,4);
            RelativeLayout rl6= addEditText(objRLContent,rl5,5);
            RelativeLayout rl7= addEditText(objRLContent,rl6,6);
            RelativeLayout rl8= addEditText(objRLContent,rl7,7);
            RelativeLayout rl9= addEditText(objRLContent,rl8,8);
            RelativeLayout rl10= addEditText(objRLContent,rl9,9);
            RelativeLayout rl11= addEditText(objRLContent,rl10,10);
            RelativeLayout rl12= addEditText(objRLContent,rl11,11);
            RelativeLayout rl13= addEditText(objRLContent,rl12,12);

         objScrollView.addView(objRLContent);  // adding contents to scroll view


         objRLScrollView.addView(objScrollView);  // Adding scroll view to a relative layout

         objRLBody.addView(objRLScrollView);  // adding relative layout to body layout


         // adding footer to body

           RelativeLayout objRLFooter=new RelativeLayout(this);
           RelativeLayout.LayoutParams objRLFooterParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,25);
           objRLFooterParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,objRLBody.getId()); 
           objRLFooter.setLayoutParams(objRLFooterParams);
           objRLFooter.setBackgroundColor(Color.parseColor("#2e4862"));
           objRLFooter.setGravity(Gravity.CENTER);
           objRLBody.addView(objRLFooter); 

           this.setContentView(objRLBody); 
  }

 private RelativeLayout addEditText(RelativeLayout objRLContent,RelativeLayout layoutAbove,int i   ) {

        RelativeLayout objRLEditText = new RelativeLayout(this); 
        objRLEditText.setId(1100+i);
        RelativeLayout.LayoutParams objRLEditTextParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if(layoutAbove!=null)
        objRLEditTextParams.addRule(RelativeLayout.BELOW,layoutAbove.getId());
        objRLEditText.setLayoutParams(objRLEditTextParams);
        objRLEditText.setPadding(8, 2, 8, 2);

         EditText objETData = new EditText(this);
         objETData.setId(1300+i);
         RelativeLayout.LayoutParams objETDataParams=new RelativeLayout.LayoutParams(200,LayoutParams.WRAP_CONTENT);
         objETData.setLayoutParams(objETDataParams);
         objETData.setPadding(8, 0, 8, 0);
         objETData.setTextSize(TypedValue.COMPLEX_UNIT_DIP,18);
         objETData.setText(""+i);
         objETData.setSingleLine(true);
         objRLEditText.addView(objETData);           

         objRLContent.addView(objRLEditText);

         return objRLEditText;
 }
}   
于 2012-07-04T10:24:11.957 回答
1

这对我很有效....

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/light_gradient">

    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

//put your UI here


    </RelativeLayout>

</ScrollView>
于 2014-10-10T11:52:38.960 回答
1

最后我通过简单的重新排列了上面代码中给body布局添加内部布局的顺序,得到了想要的布局,顺序如下

objRLBody.addView(objRLScrollView);
objRLBody.addView(objRLActionBar);
objRLBody.addView(objRLSelectedActionBar);
objRLBody.addView(objRLFooter);

还需要设置滚动视图填充,如果我们没有将填充设置为滚动视图(页眉布局宽度作为顶部填充和页脚作为底部),组件视图可能会被页眉或页脚布局阻塞。

于 2012-07-04T19:07:15.347 回答