Android - How to add several buttons with different layout_margins in a LinearLayout programmatically?

 ̄綄美尐妖づ 提交于 2019-12-25 03:43:17

问题


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:

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.

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); 

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! :)


回答1:


        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)


来源:https://stackoverflow.com/questions/29248262/android-how-to-add-several-buttons-with-different-layout-margins-in-a-linearla

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!