如何在TextView上动态添加LinearLayout

时间:2014-08-29 07:18:49

标签: android layout

我有一个应用程序,我在其中生成一个相对布局,其中包含两个文本视图,在它下面我生成一个线性布局。我希望布局在相对布局之上,但什么都没发生。请帮我解决一下。我的片段在下面。

RelativeLayout relativeLayout = null;

            LinearLayout relativeLayout1 = null;

            holder.text_data_for_Name.removeAllViews();
            for (Region object : temp.phonelist.regionList)

            {

                relativeLayout1 = new LinearLayout(getContext());
                relativeLayout1.setOrientation(LinearLayout.VERTICAL);

                relativeLayout = new RelativeLayout(getContext());

                RelativeLayout.LayoutParams relativeLayoutParams;

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

                final TextView tv2 = new TextView(getContext());

                TextView tv1 = new TextView(getContext());
                tv1.setText(object.getCCInfoLeft());
                lprams.setMargins(0, 0, 300, 0);
                tv1.setLayoutParams(lprams);

                tv1.setGravity(Gravity.LEFT);
                tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
                tv1.setPadding(13, 5, 5, 5);
                tv1.setId(1);
                relativeLayout.addView(tv1);

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


                    tv2.setText(object.getCCInfoRight());
                    // newParams.addRule(RelativeLayout.RIGHT_OF, 1);
                    newParams.setMargins(0, 0, 0, 0);
                    newParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

                    tv2.setLayoutParams(newParams);
                    tv2.setGravity(Gravity.RIGHT);
                    tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
                    tv2.setPadding(5, 5, 20, 5);
                    tv2.setId(2);
                    relativeLayout.addView(tv2);





                textView[2] = new TextView(getContext());

            relativeLayoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            textView[2].setText("");

            textView[2].setText(object.getCCInfoShortDesc());
            //textView[2].setGravity(Gravity.RIGHT);
            textView[2].setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
                String layoutDetail = object.getCCInfoShortDescAlign();
            if (layoutDetail.equals("BL")) {
                //relativeLayout1.setPadding(left, top, right, bottom);
                linearLayout.setPadding(13, 5, 5, 20);
                linearLayout.setGravity(Gravity.BOTTOM | Gravity.LEFT);
                relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, relativeLayout.getId());
                linearLayout.addView(textView[2]);//, relativeLayoutParams);

                 relativeLayout.addView(linearLayout);
            } else if (layoutDetail.equals("BR")) {
                linearLayout.setPadding(5, 5, 13, 20);
                linearLayout.setGravity(Gravity.BOTTOM | Gravity.RIGHT);
                relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, relativeLayout.getId());
                linearLayout.addView(textView[2]);
                relativeLayout.addView(linearLayout);
            } else if (layoutDetail.equals("TL")) 
            {
                linearLayout.setPadding(13, 5, 5, 20);

                relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, relativeLayout.getId());

                linearLayout.setGravity(Gravity.LEFT);

                linearLayout.addView(textView[2]);
                relativeLayout.addView(linearLayout);

            } else if (layoutDetail.equals("TR")) {
                linearLayout.setPadding(5, 5, 13, 20);
                linearLayout.setGravity(Gravity.TOP | Gravity.RIGHT);
                linearLayout.addView(textView[2]);//, relativeLayoutParams);
                relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, relativeLayout.getId());
                linearLayout.addView(textView[2]);
                 relativeLayout.addView(linearLayout);
            }

            holder.tex

t_data_for_Name.addView(RelativeLayout的);

2 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

        RelativeLayout relativeLayout = null;
        LinearLayout linearLayout = null;

        int linearId=11;
        holder.text_data_for_Name.removeAllViews();
        for (Region object : temp.phonelist.regionList) {

            relativeLayout = new RelativeLayout(getContext());
            linearLayout = new LinearLayout(getContext());
            linearLayout.setOrientation(LinearLayout.VERTICAL);
            linearLayout.setId(linearId++);

            textView[2] = new TextView(getContext());
            textView[2].setText("");
            textView[2].setText(object.getCCInfoShortDesc());
            textView[2].setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);

            RelativeLayout.LayoutParams linearLayoutprams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams tv1prams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams tv2prams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

            final TextView tv2 = new TextView(getContext());
            TextView tv1 = new TextView(getContext());
            tv1.setText(object.getCCInfoLeft());
            tv1prams.setMargins(0, 0, 300, 0);
            tv1.setGravity(Gravity.LEFT);
            tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            tv1.setPadding(13, 5, 5, 5);
            tv1.setId(linearId++);


            tv2.setText(object.getCCInfoRight());
            tv2prams.setMargins(0, 0, 0, 0);
            tv2.setGravity(Gravity.RIGHT);
            tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            tv2.setPadding(5, 5, 20, 5);
            tv2.setId(linearId++);

            String layoutDetail = object.getCCInfoShortDescAlign();
            if (layoutDetail.equals("BL")) {
                //relativeLayout1.setPadding(left, top, right, bottom);
                linearLayout.setPadding(13, 5, 5, 20);
                linearLayout.setGravity(Gravity.BOTTOM | Gravity.LEFT);
                linearLayoutprams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                linearLayout.addView(textView[2]);//, relativeLayoutParams);
                relativeLayout.addView(linearLayout);
                tv1prams.addRule(RelativeLayout.ABOVE,linearLayout.getId());
                tv1prams.addRule(RelativeLayout.ALIGN_LEFT);
                tv2prams.addRule(RelativeLayout.ABOVE,linearLayout.getId());
                tv2prams.addRule(RelativeLayout.ALIGN_RIGHT);
            } else if (layoutDetail.equals("BR")) {
                linearLayout.setPadding(5, 5, 13, 20);
                linearLayout.setGravity(Gravity.BOTTOM | Gravity.RIGHT);
                linearLayoutprams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                linearLayout.addView(textView[2]);
                relativeLayout.addView(linearLayout);
                tv1prams.addRule(RelativeLayout.ABOVE,linearLayout.getId());
                tv1prams.addRule(RelativeLayout.ALIGN_LEFT);
                tv2prams.addRule(RelativeLayout.ABOVE,linearLayout.getId());
                tv2prams.addRule(RelativeLayout.ALIGN_RIGHT);
            } else if (layoutDetail.equals("TL")) {
                linearLayout.setPadding(13, 5, 5, 20);
                linearLayoutprams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                linearLayout.setGravity(Gravity.LEFT);
                linearLayout.addView(textView[2]);
                relativeLayout.addView(linearLayout);
                tv1prams.addRule(RelativeLayout.BELOW,linearLayout.getId());
                tv1prams.addRule(RelativeLayout.ALIGN_LEFT);
                tv2prams.addRule(RelativeLayout.BELOW,linearLayout.getId());
                tv2prams.addRule(RelativeLayout.ALIGN_RIGHT);
            } else if (layoutDetail.equals("TR")) {
                linearLayout.setPadding(5, 5, 13, 20);
                linearLayout.setGravity(Gravity.TOP | Gravity.RIGHT);
                linearLayout.addView(textView[2]);//, relativeLayoutParams);
                linearLayoutprams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                linearLayout.addView(textView[2]);
                relativeLayout.addView(linearLayout);
                tv1prams.addRule(RelativeLayout.BELOW,linearLayout.getId());
                tv1prams.addRule(RelativeLayout.ALIGN_LEFT);
                tv2prams.addRule(RelativeLayout.BELOW,linearLayout.getId());
                tv2prams.addRule(RelativeLayout.ALIGN_RIGHT);
            }
            tv2prams.addRule(RelativeLayout.RIGHT_OF,tv1.getId());
            tv1.setLayoutParams(tv1prams);
            relativeLayout.addView(tv1);

            tv2.setLayoutParams(tv2prams);
            relativeLayout.addView(tv2);
        }

答案 1 :(得分:0)

我认为你需要将relativeLayoutParams设置为relativeLayout1。

relativeLayout1.setLayoutParams(relativeLayoutParams)

希望它有效。