如何通过单击按钮将自定义布局添加到线性布局

时间:2021-01-11 03:15:10

标签: java android

所以我正在尝试创建一个任务应用程序,在其中单击一个按钮,我希望显示我的子任务自定义视图,其中包含一个文本视图和 2 个图像视图,在水平线性布局中在垂直线性布局内。子任务所需的数据取自不同的活动,然后使用意图进行传输。另外,我需要添加不同的每个子任务的 id。我不知道该怎么做。请帮忙。我无法为此编写任何代码。我尝试使用 layout inflater,在网上搜索要做什么,但我仍然不知道。请帮忙。

2 个答案:

答案 0 :(得分:0)

您可以为此使用对话框片段,并且看起来也很有效 给个链接吧... dialog fragment

答案 1 :(得分:0)

我在我的项目中做了类似的事情,以编程方式在垂直线性布局中添加水平线性布局。

请检查以下代码:

View view = View.inflate(getActivity(), R.layout.item_horizontal_layout, null);
    
Button btnSave = view.findViewById(R.id.btnSaveModule);

layoutCount += 1; // A global value to store added layout counts

btnSave.setTag(layoutCount); // Set the tag on button so that we can use it later

btnSave.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Access the layout position on button click
        Log.d("Layout Position", ":" + (int)btnSave.getTag())
    }
});

verticalLL.addView(view); // Add horizontal layout to vertical Linear Layout