【问题标题】:How do I make my Bottom Sheet Dialog dynamic?如何使我的底部工作表对话框动态化?
【发布时间】:2021-12-25 11:06:29
【问题描述】:

这是我第一次尝试在我的 Android Studio 项目中实现 BottomSheetDialog。为了更熟悉这个过程,我尝试在 Youtube 上遵循本教程:https://youtu.be/hfoXhiMTc0c。在我的实际 Java 类中,当我扫描包含不同信息的 NFC 芯片时,将激活 BottomSheet。但是我无法在工作表上动态显示来自芯片的信息。我猜这是由于工作表是静态的?我如何能够显示来自芯片的信息,这些信息已经存储在我的 Java 类的变量中,以显示在 BottomSheet 的文本字段中?

感谢您的帮助,谢谢!

这是扩展BottomSheet的java类的代码sn-p:

final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(

                Scan.this, R.style.BottomSheetDialogTheme
        );
        View bottomSheetView = LayoutInflater.from(getApplicationContext())
                .inflate(
                        R.layout.layout_bottom_sheet,
                        (LinearLayout)findViewById(R.id.bottomSheetContainer)

                );
        bottomSheetView.findViewById(R.id.addToCloset).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomSheetDialog.dismiss();
            }
        });
        bottomSheetDialog.setContentView(bottomSheetView);
        bottomSheetDialog.show();```

【问题讨论】:

    标签: java android xml android-bottomsheetdialog


    【解决方案1】:

    我不熟悉BottomSheetDialog。但是,

            View bottomSheetView = LayoutInflater.from(getApplicationContext())
                .inflate(
                        R.layout.layout_bottom_sheet,
                        (LinearLayout)findViewById(R.id.bottomSheetContainer)
    
                );
    

    您应该可以将上面的代码替换为,

        View bottomSheetView = LayoutInflater.from(getApplicationContext())
            .inflate(
                    R.layout.layout_bottom_sheet,
                    null
    
            );
    

    现在转到 layout_bottom_sheet.xml 布局文件。根据您的代码,它应该具有没有 id 的线性布局。给它一个id。我将把那个 id 作为“test_ll”。

    现在下面的代码就可以了,

    LinearLayout ll = bottomSheetView.findViewById(R.id.test_ll);
    

    之后,您可以将视图动态添加到 ll。如需动态添加视图到 LinearLayout,请参考,

    Add text view to Linear layout

    编辑:

    如果你想在 LinearLayout 中使用视图,你可以这样做,

    View view = ll.findViewById(R.id.view_id);
    

    如果你的文本视图在 ll 中,

    TextView textView1 = ll.findViewById(R.id.tvcolor);       
    textView1.setText("Hello!!");
    

    这会解决你的问题。

    【讨论】:

    • 完美,非常感谢!除了一件事之外,一切现在都在工作。您好心发给我的链接不太适合我的问题,因为我只是想访问 .xml 文件的现有 TextView 而不是尝试创建新的。我尝试使用:TextView textView1 = (TextView)findViewById(R.id.tvcolor); textView1.setText("Hello!!"); 但是,当尝试运行代码时,应用程序正在崩溃:(。关于如何修复的任何想法?
    • 该文本视图是否在 LinearLayout 中?
    • 检查我答案的编辑部分。它可能会解决您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    相关资源
    最近更新 更多