动态地向Fragments添加按钮 [英] Adding buttons to Fragments dynamically

查看:142
本文介绍了动态地向Fragments添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试动态地向我的片段添加按钮,但我尝试过的所有方法都不起作用。

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;

    }

这将给我以下截图第一种方法
我真的不喜欢这个,因为这会在Activity上创建一个按钮,它会出现在我使用相同Activity的其他Fragment上。

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. 第二种方法

public View onCreateView(LayoutInflater inflater,@ Nullable ViewGroup容器,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;
}

我假设这个方法会生成按钮(因为代码运行正常且在至少设置OnClickListner()时按钮没有空指针引用。但是我无法在屏幕上看到实际按钮。此外,无法点击它。

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.

我试图从这些
网址中引用:以编程方式将按钮添加到片段

以编程方式向片段添加按钮

如何以编程方式向片段添加按钮

任何帮助我将非常感激。

Any help will be really appreciated.

推荐答案

最后,我已经成功了第二种方法有问题,因为没有

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

btn = myView.findViewById(id _);
这将指向null虽然我已将我的按钮的ID设置为我所说的值。使用此方法,通过设置单独的 onClick()方法,很容易获得按钮的值。

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

这篇关于动态地向Fragments添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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