首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动态布局问题在布局中添加填充

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

Stack Overflow用户
提问于 2014-12-05 21:30:38
回答 1查看 98关注 0票数 0

我有一个LinearLayout对象,我想用一个动态添加的图像对象(它动态地改变它的颜色)来动态改变背景图像。

代码如下:

代码语言:javascript
运行
复制
            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);
                        }
                    }
                }
            }'
EN

回答 1

Stack Overflow用户

发布于 2014-12-06 13:23:23

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

这是关于9-patches的文档:http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

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

我想您也可以尝试在setBackgroundResource之后调用setPadding,但我不确定这是否有效。真的,你应该在设计你的背景时考虑到你想要的填充。

这有意义吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27316998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档