Adding buttons to Fragments dynamically

2019-02-26 15:22发布

I have been trying to add buttons to my fragment dynamically but all the methods I have tried somehow doesnt work.

These are some methods I have tried:

1.

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

LinearLayout linearlayout = new LinearLayout(getActivity());

        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        linearlayout.setLayoutParams(buttonParams);
        linearlayout.setOrientation(LinearLayout.HORIZONTAL);

        Button button = new Button(getActivity());
        button.setLayoutParams(buttonParams);
        button.setText("????????????????????");
        button.setTextSize(16);

        Button button2 = new Button(getActivity());
        button2.setLayoutParams(buttonParams);
        button2.setText("!!!!!!!!!!!!!!!!!!");
        button2.setTextSize(64);

        linearlayout.addView(button);
        linearlayout.addView(button2);

        container.addView(linearlayout);
        View myView = inflater.inflate(R.layout.fragment_general_layout, container, false);

 return myView;

    }

This will give me the following screenshot First method. I really dont like this as this will create a button on the Activity itself which would appear on my other Fragments that are using the same Activity.

  1. For second method

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

View myView = inflater.inflate(R.layout.fragment_general_layout, container, false);


            for (int i = 0; i < ArrayOfNames.length; i++) {

                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);

                Button btn = new Button(myView.getContext());
                btn.setId(i);
                final int id_ = btn.getId();
                btn.setText(ArrayOfNames[i]);
                btn.setBackgroundColor(Color.CYAN);
                btn.setLayoutParams(params);
                linearlayout.addView(btn, params);
                btn = myView.findViewById(id_);

                btn.setVisibility(View.VISIBLE);

                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            //do smth


                    }
                });
return myView;
}

This method I assume will make the buttons (as the code runs fine and at least the buttons are not having a null pointer references when setting the OnClickListner(). But I cant see actual buttons on my screen. Also, not possible to click on it.

I tried to reference from these urls: Programmatically add buttons to a Fragment

Adding buttons programmatically to a fragment

How to add a button programmatically to a fragment

Any help will be really appreciated.

2条回答
来,给爷笑一个
2楼-- · 2019-02-26 15:33

In the end, I have manged to do it. The second method had problem as there is no

btn = myView.findViewById(id_);. This will be pointing to null although I have set my button's ID to be the value I stated. Using this method, it is easy to still get the value of the button by setting a separate onClick() method.

            for (int i = 0; i < ArrayOfNames.length; i++) {

                final Button myButton = new Button(myView.getContext());
                myButton.setText(RAnames[i]);
                myButton.setId(i + 1);
                myButton.setOnClickListener(this);

                myButton.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                myButton.setTextSize(18);
                myButton.setPadding(20, 0, 20, 0);

                LinearLayout linearlayout = (LinearLayout) myView.findViewById(R.id.btnholder);
                linearlayout.setOrientation(LinearLayout.VERTICAL);

                LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                buttonParams.setMargins(0, 0, 0, 10);

                linearlayout.addView(myButton, buttonParams);
}
查看更多
太酷不给撩
3楼-- · 2019-02-26 15:44

The onCreateView method expects you to return the view which you are inflating. Your code is good, but you're adding the buttons to the wrong view. Try this instead:

//container.addView(linearlayout);
myView = inflater.inflate(R.layout.fragment_general_layout, container, false);
myView.addView(linearlayout);

This should add your buttons to your view. I'm assuming that your R.layout.fragment_general_layout is a LinearLayout with orientation="vertical"

查看更多
登录 后发表回答