动态布局问题在布局中添加填充

时间:2014-12-05 13:30:38

标签: android android-layout android-fragments

我有一个LinearLayout对象,想要动态添加图像对象动态更改背景图像(动态更改颜色)。

这是代码:

            if (categoryResponse != null && categoryResponse.result != null
                    && categoryResponse.result.length > 0) {

                int i = 0;
                category = new TextView[categoryResponse.result.length];
                for (Category cat : categoryResponse.result) {

                    category[i] = new TextView(getActivity());
                    LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.WRAP_CONTENT);

                    par.setMargins(0, 10, 0, 0);
                    category[i].setLayoutParams(par);

                    category[i].setGravity(Gravity.CENTER);
                    category[i].setText(cat.name);
                    category[i].setTag(cat.id);

                    category[i].setPadding(0, 10, 0, 10);
                    category[i].setTextSize(25);
                    category[i].setBackgroundResource(R.drawable.category);
                    category[i].setTypeface(Global.AppsFont);
                    category[i].setTextColor(getResources().getColor(
                            R.color.white));


                    main.addView(category[i]);

                    //System.out.println("header textview width = " +category[i].getWidth() + "");

                    category[i].setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            String id = (String) v.getTag();

                            for (Entry<String, LinearLayout> entry : channels
                                    .entrySet()) {

                                if (entry.getKey().equals(id)) {
                                    entry.getValue().setVisibility(
                                            View.VISIBLE);
                                } else
                                    entry.getValue().setVisibility(
                                            View.GONE);
                            }
                        }
                    });
                    i++;

                    LinearLayout LLM = null;
                    LLM = new LinearLayout(getActivity());

                    channels.put(cat.id, LLM);

                    LLM.setOrientation(LinearLayout.VERTICAL);
                    LLM.setVisibility(View.GONE);

                    LinearLayout.LayoutParams LLParamsm = new LinearLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.MATCH_PARENT);

                    LLParamsm.setMargins(0, 0, 0, 0);
                    LLM.setPadding(0, 0, 0, 0);
                    LLM.setLayoutParams(LLParamsm);

                    LLM.setBackgroundResource(R.drawable.category_bg);


                    main.addView(LLM);
                    LinearLayout LL = null;

                    int j = 0;
                    ChannelResponse channelResponse = JsonUtils
                            .getChannel(cat.id);

                    channelResponseData.put(cat.id, channelResponse);
                    if (channelResponse != null
                            && channelResponse.result != null
                            && channelResponse.result.length > 0) {

                        for (Channel chan : channelResponse.result) {

                            if (j % 2 == 0) {
                                LL = new LinearLayout(getActivity());


                                LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(
                                        LayoutParams.MATCH_PARENT,
                                        LayoutParams.MATCH_PARENT);

                                LL.setOrientation(LinearLayout.HORIZONTAL);
                                LL.setWeightSum(1);

                                LL.setLayoutParams(LLParams);
                                LLM.addView(LL);                                    
                            }

                            Button chn = new Button(getActivity());
                            LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(
                                    LayoutParams.MATCH_PARENT,
                                    LayoutParams.MATCH_PARENT, 0.5f);
                            ladderFLParams.setMargins(10, 10, 10, 10);
                            chn.setBackgroundResource(R.drawable.gray_selector);
                            chn.setGravity(Gravity.CENTER);
                            chn.setLayoutParams(ladderFLParams);
                            chn.setText(chan.name);
                            chn.setTag(chan.id);
                            chn.setTextSize(25);
                            chn.setTextColor(getResources().getColor(
                                    R.color.black));
                            chn.setTypeface(Global.AppsFont);
                            chn.setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    channelIdid = (String) v.getTag();
                                    createDialogBox(v);
                                }

                            });

                            LL.addView(chn);
                            channel.add(chn);

                            j++;
                        }

                        if (j % 2 != 0) {

                            Button chn = new Button(getActivity());
                            LinearLayout.LayoutParams ladderFLParams = new LinearLayout.LayoutParams(
                                    LayoutParams.MATCH_PARENT,
                                    LayoutParams.WRAP_CONTENT, 0.5f);
                            ladderFLParams.setMargins(10, 10, 10, 10);

                            chn.setBackgroundResource(R.drawable.gray_selector);
                            chn.setGravity(Gravity.CENTER_HORIZONTAL
                                    | Gravity.CENTER_VERTICAL);
                            chn.setLayoutParams(ladderFLParams);
                            chn.setVisibility(View.INVISIBLE);
                            chn.setTextSize(25);
                            chn.setTextColor(getResources().getColor(
                                    R.color.black));
                            chn.setTypeface(Global.AppsFont);

                            LL.addView(chn);
                            channel.add(chn);
                        }
                    }
                }
            }'

1 个答案:

答案 0 :(得分:0)

9-patch图像会自动添加内置于9-patch中的填充,作为填充到视图中。因此,当您致电LLM.setPadding(0, 0, 0, 0)时,当您拨打被覆盖的LLM.setBackgroundResource时。

以下是有关9补丁的文档:http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

如果您不想填充,最好的选择是设计没有填充的9补丁,换句话说,可绘制和可伸展区域都会到达图像的边缘。

我想你也可以尝试在setBackgroundResource之后调用setPadding,但我不确定这是否可行。真的,你应该只考虑所需的填充设计你的背景。

这有意义吗?