java - 动态向 Fragment 添加按钮

标签 java android android-fragments button

我一直在尝试动态地将按钮添加到我的 fragment 中,但我尝试过的所有方法都不起作用。

以下是我尝试过的一些方法:

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;

    }

这将为我提供以下屏幕截图 First method 。 我真的不喜欢这个,因为这会在 Activity 本身上创建一个按钮,该按钮会出现在使用相同 Activity 的其他 fragment 上。

  • 第二种方法
  • public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup容器,Bundle savingInstanceState){

    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() 时按钮没有空指针引用。但我看不到上的实际按钮我的屏幕。而且,无法点击它。

    我尝试引用这些 网址:Programmatically add buttons to a Fragment

    Adding buttons programmatically to a fragment

    How to add a button programmatically to a fragment

    任何帮助将不胜感激。

    最佳答案

    onCreateView 方法期望您返回正在膨胀的 View 。您的代码很好,但是您将按钮添加到了错误的 View 。试试这个:

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

    这应该将您的按钮添加到您的 View 中。我假设您的 R.layout.fragment_general_layout 是一个带有 orientation="vertical"

    LinearLayout

    关于java - 动态向 Fragment 添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50210994/

    相关文章:

    android - fragment 中的 OnPostCreate

    android - 多 fragment 最佳实践

    android - 如何使用 kivy StringProperty?

    android - 创建 fragment : constructor vs newInstance()

    java - 是否可以在 Android 中以 dd-mm-yyyy 格式获取 Date 对象?

    java - 将 Base64 编码的 md5 转换为可读字符串

    java - 有效地存储和查询字符串列表

    java - 内部 JNLP 应用程序无法在 Java 7 下运行

    android - 带有异步任务的 getInstalledApplications()

    android - 错误 : Could not get unknown property 'iosX64' for KotlinTargetPreset in Android Studio 3. 1.4?