Android-如何以编程方式在LinearLayout中添加具有不同layout_margins的多个按钮? [英] Android - How to add several buttons with different layout_margins in a LinearLayout programmatically?

查看:99
本文介绍了Android-如何以编程方式在LinearLayout中添加具有不同layout_margins的多个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式构建线性布局,其中共有4个按钮,每个按钮都位于上一个按钮的下方.就像下面的图片一样:

I am trying to building a linear layout programmatically, in which, there are 4 buttons in all, each of the button simply positioned below the previous button. Just like the picture shows below:

从上图中可以看到,每个按钮的大小完全相同,但是具有不同的layout_margins,第一个按钮在layout_marginTop中具有较大的值,而其他3个按钮在layout_marginTop中具有相同的值.

And as you can see from the picture above, each button has exactly the same size but they have different layout_margins, the first button has a larger value in layout_marginTop while the other 3 buttons has the same value in layout_marginTop.

基本上,通过xml构建这样的布局非常简单,但是现在我真的遇到了仅通过java代码构建所有布局的困难.我已经读了很多文章,现在我可以轻松地添加4个大小合适的按钮,但是我找不到以编程方式为每个按钮设置layout_margin的正确方法.

Basically to build a layout like this by xml is very simple but now I have really come across difficulties in building all this only through java code. I have gone through many posts and now I can easily add 4 buttons in proper sizes but I just could not find a proper way to programmatically set the layout_margin for each of the button.

要简单地添加四个按钮,我可以这样:

To simple add four buttons, I could do like this:

public class mainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);  //Can also be done in xml by android:orientation="vertical"

    for (int i = 0; i < 3; i++) {
        LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        for (int j = 0; j < 4; j++) {
            Button btnTag = new Button(this);
            btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            btnTag.setText("Button " + (j + 1 + (i * 4 )));
            btnTag.setId(j + 1 + (i * 4));
            row.addView(btnTag);
        }

        layout.addView(row);
    }
    setContentView(layout);
    //setContentView(R.layout.main);
}
}

然后以编程方式为每个按钮(视图)设置边距,我可以这样做:

And to programmatically set margins for each button(view), I could do like this:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 2, 0, 0);
button.setLayoutParams(params); 

但是问题来了:只要我为4个按钮设置了LayoutParams,然后通过"addView(button)"将4个按钮添加到Linearlayout中,则只有第一个按钮可以以适当的大小和适当的边距显示.其他三个按钮都消失了.而且,通过许多测试,我发现似乎只能在linearlayout对象中允许一个layoutParams.结果,只要为不同的按钮设置不同的布局参数,就只能显示第一个按钮.但是由于这里的4个按钮的边距参数肯定不同,因此我认为我必须对不同的按钮使用不同的layoutparams.

But here comes the problem: as long as I setLayoutParams for the 4 buttons, and then add 4 buttons into Linearlayout by "addView(button)", only the first button could be displayed in the proper size with the proper margins. All the other 3 buttons just disappeared. And With many tests I just found it seems that only one layoutParams could be allowed in a linearlayout object. As a result, as long as I set different layout params for different buttons, only the first button could be displayed. But since here the margin params for my 4 buttons are definitely different thus I think I have to use different layoutparams for different buttons.

所以请问有人告诉我如何以编程方式为我的每个按钮设置边距并使它们正确显示?这已经吸了我两天的生命,请帮助! :)

So pls anyone tell me how could I programmatically set the margin for my each of my buttons and make them display properlly? This has already sucked my life for two days, pls help! :)

推荐答案

        linear = (LinearLayout) rootView.findViewById(R.id.linear);
    .
    .
    .   

     RelativeLayout.LayoutParams layoutParam = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.MATCH_PARENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
                    LinearLayout layout = new LinearLayout(getActivity());
                    layout.setLayoutParams(layoutParam);
                    layout.setOrientation(LinearLayout.VERTICAL);

                    // Below will add three linear layout with 4 buttons in each
                    for (int i = 0; i < 3; i++) {
                        LinearLayout row = new LinearLayout(getActivity());
                        row.setLayoutParams(new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT));
//Here is important
                        row.setOrientation(LinearLayout.VERTICAL);

                        for (int j = 0; j < 4; j++) {
                            Button btnTag = new Button(getActivity());
                            btnTag.setLayoutParams(new LayoutParams(
                                    LayoutParams.WRAP_CONTENT,
                                    LayoutParams.WRAP_CONTENT));
                            btnTag.setText("Button " + (j + 1 + (i * 4)));
                            btnTag.setId(j + 1 + (i * 4));
                            row.addView(btnTag);
                        }
                        layout.addView(row);
                    }
                    linear.addView(layout);
                    // You can set also
                    // setContentView(layout)

这篇关于Android-如何以编程方式在LinearLayout中添加具有不同layout_margins的多个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆