android以编程方式创建带有表头页脚的布局

时间:2016-12-21 13:36:12

标签: android layout programmatically-created

我需要使用以下格式创建fragment

Header位于顶部,然后是table,然后是footer

我需要以编程方式创建布局。 我有以下用于创建模板的代码。

它拥有我需要的一切,但桌子仍占用整个屏幕,除非我取消注释vscoll填充。

enter image description here

 // Setup

        int id_mainrelativelayout   = Menu.FIRST + 200;
        int id_headerrelativelayout   = Menu.FIRST + 201;
        int id_middlerelativelayout   = Menu.FIRST + 202;
        int id_footerrelativelayout   = Menu.FIRST + 203;
        int id_header1   = Menu.FIRST + 204;
        int id_footer1   = Menu.FIRST + 205;
        int id_textview0 = Menu.FIRST + 210;
        int id_textview1 = Menu.FIRST + 215;
        int id_textview2 = Menu.FIRST + 220;
        int id_textview3 = Menu.FIRST + 225;
        int id_body      = Menu.FIRST + 230;
        int id_switch    = Menu.FIRST + 235;
        int id_mytable   = Menu.FIRST + 240;
        int id_tablerow  = Menu.FIRST + 246;

        RelativeLayout.LayoutParams linearvparam   = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        RelativeLayout.LayoutParams linearh1param  = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        RelativeLayout.LayoutParams linearh2param  = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        RelativeLayout.LayoutParams linearh3param  = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout.LayoutParams textview0param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams textview1param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams textview2param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        RelativeLayout.LayoutParams textview3param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout.LayoutParams imageview1param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        RelativeLayout.LayoutParams tableDetails= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        TableRow.LayoutParams tablerowparam10 = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f);
        TableRow.LayoutParams tablerowparam08 = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.8f);
        TableRow.LayoutParams tablerowparam05 = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5f);
        TableRow.LayoutParams tablerowparam01 = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.1f);
        TableRow.LayoutParams tablerowparam00 = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.0f);
        //tablerowparam10.topMargin = 220;
        //tablerowparam10.span = 1;

        // Layout

        RelativeLayout mainrelativelayout = new RelativeLayout(getActivity());
        RelativeLayout headerrelativelayout = new RelativeLayout(getActivity());
        RelativeLayout middlerelativelayout = new RelativeLayout(getActivity());
        RelativeLayout footerrelativelayout = new RelativeLayout(getActivity());

        RelativeLayout.LayoutParams mainrelativelayoutparam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        RelativeLayout.LayoutParams headerrelativelayoutparam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams middlerelativelayoutparam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams footerrelativelayoutparam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        mainrelativelayout.setId(id_mainrelativelayout);
        headerrelativelayout.setId(id_headerrelativelayout);
        middlerelativelayout.setId(id_middlerelativelayout);
        footerrelativelayout.setId(id_footerrelativelayout);

        headerrelativelayoutparam.addRule(mainrelativelayout.ALIGN_PARENT_TOP);
        footerrelativelayoutparam.addRule(mainrelativelayout.ALIGN_PARENT_BOTTOM);
        middlerelativelayoutparam.addRule(mainrelativelayout.BELOW,headerrelativelayout.getId());
        middlerelativelayoutparam.addRule(mainrelativelayout.ABOVE,footerrelativelayout.getId());

        LinearLayout header1linearlayout = new LinearLayout(getActivity());
        LinearLayout middle1linearlayout = new LinearLayout(getActivity());
        LinearLayout footer1linearlayout = new LinearLayout(getActivity());
        LinearLayout.LayoutParams header1linearlayoutparam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams middle1linearlayoutparam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams footer1linearlayoutparam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);

        //header1linearlayout.setPadding(00,00,00,00);
        //middle1linearlayout.setPadding(00,00,00,00);
        //footer1linearlayout.setPadding(00,00,00,00);

        TableLayout maintable = new TableLayout (getActivity());
        maintable.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
        maintable.setWeightSum(1);
        maintable.setStretchAllColumns(true);
        maintable.setColumnShrinkable(0, true);
        maintable.setColumnStretchable(0, true);
        maintable.setColumnShrinkable(1, true);
        maintable.setColumnStretchable(1, true);
        maintable.removeAllViews();

        RelativeLayout.LayoutParams header1param   = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        TextView header1 = new TextView(getActivity());
        header1.setId(id_header1);
        header1.setText("Header");
        header1.setTextSize(30);
        header1.setPadding(00,00,00,00);
        header1.setSingleLine(false);
        header1.setTextColor(Color.BLACK);
        header1.setGravity(Gravity.CENTER);
        //header1param.addRule(RelativeLayout.CENTER_HORIZONTAL);
        //header1.setLayoutParams(matchmatch);
        header1linearlayout.addView(header1,header1param);

        for (int i = 0; i < 25; i++) {

            TableLayout.LayoutParams tableRowParams= new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT);
            tableRowParams.setMargins(00, 00, 00, 10); //left,top,right,bottom
            TableRow tablerow = new TableRow(getActivity());
            tablerow.setId(i);
            tablerow.setLayoutParams(tableRowParams);
            tablerow.isClickable();
            tablerow.setOnClickListener(new View.OnClickListener()
            {
                public void onClick(View v)
                {
                    commonfunc.myprint("#######" + v.getId());
                    v.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
                }
            });

            LinearLayout linearv = new LinearLayout(getActivity());
            linearv.setOrientation(LinearLayout.VERTICAL);
            linearv.setPadding(00,00,00,00);

            LinearLayout linearh1 = new LinearLayout(getActivity());
            linearh1.setOrientation(LinearLayout.HORIZONTAL);
            linearh1.setPadding(00,00,00,00);

            LinearLayout linearh2 = new LinearLayout(getActivity());
            linearh2.setOrientation(LinearLayout.HORIZONTAL);
            linearh2.setPadding(00,00,00,00);

            LinearLayout linearh3 = new LinearLayout(getActivity());
            linearh3.setOrientation(LinearLayout.HORIZONTAL);
            linearh3.setBackgroundColor(Color.BLACK);
            linearh3param.height = 5;
            linearh3.setPadding(00,00,00,00);
            linearh3.setLayoutParams(linearh3param);

            //Row1
            RelativeLayout calender_image_text = new RelativeLayout(getActivity());
            ImageView imageview1 = new ImageView(getActivity());
            imageview1.setBackgroundResource(R.mipmap.calendar);
            imageview1.setPadding(00,00,00,00);
            //imageview1.setMaxHeight(150);
            //imageview1.setMaxWidth(150);
            LinearLayout.LayoutParams imageview2param = new LinearLayout.LayoutParams(120, 80);
            imageview1param.addRule(RelativeLayout.CENTER_IN_PARENT);
            calender_image_text.addView(imageview1,imageview2param);
            //linearh1.addView(calender_image_text,tablerowparam00);

            TextView textview0 = new TextView(getActivity());
            textview0.setId(id_textview0);
            textview0.setText("Dec 0" + i);
            //textview0.setTextSize(12);
            textview0.setPadding(00,00,00,00);
            textview0.setSingleLine(false);
            textview0.setTextColor(Color.BLACK);
            //textview0.setGravity(Gravity.LEFT);
            //textview0param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            //textview0param.addRule(RelativeLayout.LEFT_OF,imageview1.getId());
            //textview0param.addRule(RelativeLayout.RIGHT_OF,imageview1.getId());
            //textview0param.addRule(RelativeLayout.ALIGN_TOP,imageview1.getId());
            textview0param.addRule(RelativeLayout.CENTER_IN_PARENT,imageview1.getId());
            textview0.setGravity(Gravity.CENTER);
            textview0.setLayoutParams(textview0param);
            calender_image_text.addView(textview0,textview0param);
            linearh1.addView(calender_image_text,tablerowparam00);

            // or

            TextView textview00 = new TextView(getActivity());
            textview00.setId(id_textview1);
            textview00.setText("Dec 25");
            textview00.setBackgroundResource(R.mipmap.calendar);
            //textview1.setTextSize(20);
            textview00.setPadding(00,00,00,00);
            textview00.setSingleLine(false);
            textview00.setTextColor(Color.BLACK);
            textview00.setGravity(Gravity.CENTER);
            textview00.setLayoutParams(textview1param);
            //linearh1.addView(textview00,tablerowparam01);

            TextView textview1 = new TextView(getActivity());
            textview1.setId(id_textview1);
            textview1.setText("RowOneLeft " + i);
            //textview1.setTextSize(20);
            textview1.setPadding(00,00,00,00);
            textview1.setSingleLine(false);
            textview1.setTextColor(Color.BLACK);
            textview1.setGravity(Gravity.LEFT);
            textview1param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            textview1.setLayoutParams(textview1param);
            linearh1.addView(textview1,tablerowparam08);

            TextView textview2 = new TextView(getActivity());
            textview2.setId(id_textview2);
            textview2.setText("RowOneRight " + i);
            //textview2.setTextSize(20);
            textview2.setPadding(00,00,00,00);
            textview2.setSingleLine(true);
            textview2.setTextColor(Color.BLACK);
            textview2.setGravity(Gravity.RIGHT);
            textview2param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            textview2.setLayoutParams(textview2param);
            linearh1.addView(textview2,tablerowparam01);

            //Row2

            TextView textview3 = new TextView(getActivity());
            textview3.setId(id_textview3);
            textview3.setText("This Needs To Wrap  This Needs To Wrap This Needs To Th12345678is Needs To WrapThis Needs To Wrap  This Needs To Wrap This Needs To This Needs To WrapEnd  " + i);
            //textview3.setTextSize(20);
            textview3.setPadding(00,00,00,00);
            textview3.setSingleLine(false);
            textview3.setTextColor(Color.BLACK);
            textview3.setGravity(Gravity.LEFT);
            textview3param.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            textview3.setLayoutParams(textview3param);
            linearh2.addView(textview3,tablerowparam10);

            linearv.addView(linearh1,linearh1param);  // image subject switch
            linearv.addView(linearh2,linearh2param);  // body
            linearv.addView(linearh3,linearh3param);  // line

            tablerow.addView(linearv);

            maintable.addView(tablerow);
        }

        //Set padding order by Left, Top, Right and Bottom side in pixels.
        RelativeLayout.LayoutParams footer1param  = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        TextView footer1 = new TextView(getActivity());
        footer1.setId(id_footer1);
        footer1.setText("Footer");
        footer1.setTextSize(30);
        footer1.setPadding(00,00,00,00);
        footer1.setSingleLine(false);
        footer1.setTextColor(Color.BLACK);
        footer1.setGravity(Gravity.CENTER);
        footer1param.addRule(RelativeLayout.CENTER_HORIZONTAL);
        //footer1.setLayoutParams(matchmatch);
        footer1linearlayout.addView(footer1,footer1param);

        RelativeLayout.LayoutParams scrollparam   = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        ScrollView vscroll = new ScrollView(getActivity());
        vscroll.setFillViewport(true);
        vscroll.setPadding(00,180,00,180);
        vscroll.addView(maintable,tableDetails);

        headerrelativelayout.addView(header1linearlayout,header1param);
        middlerelativelayout.addView(vscroll,scrollparam);
        footerrelativelayout.addView(footer1linearlayout,footer1param);

        mainrelativelayout.addView(headerrelativelayout,headerrelativelayoutparam); // (child,it's higher parent)
        mainrelativelayout.addView(middlerelativelayout,mainrelativelayoutparam);
        mainrelativelayout.addView(footerrelativelayout,footerrelativelayoutparam);

        view =mainrelativelayout;

        return view;
    }

2 个答案:

答案 0 :(得分:0)

如果你把这个

TableLayout maintable = new TableLayout (getActivity());
maintable.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));

它将使用完整的窗口,尝试此设置指定的大小,如“500”,“700”,如果它工作,你将需要设置屏幕的大小 - 页脚的大小和标题

在我的代码中我使用:

RelativeLayout fullWindow = (RelativeLayout) findViewById(R.id.fullwindow);
int windowHeight = fullWindow.getHeight() / 2;
popupWindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT, windowHeight);

只有一个问题,为什么你需要以编程方式完成所有事情?

答案 1 :(得分:0)

代码中的相对布局太多,只减少到2个。