简体   繁体   English

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

[英]how do I add a custom layout to a Linear Layout on the click of a button

So I am trying to create a task app, in which, on the click of a button, I want my subtask custom view, which contains a text view, and 2 image views, inside a horizontal linear layout, to be displayed inside a vertical linear layout.所以我正在尝试创建一个任务应用程序,在其中,单击一个按钮,我希望我的子任务自定义视图,其中包含一个文本视图和 2 个图像视图,在水平线性布局内,显示在垂直线性布局。 The data required for the subtask is taken from a different activity, and then transferred using intents.子任务所需的数据取自不同的活动,然后使用意图传输。 Also, I need the id for every subtask that is added to be different.此外,我需要添加不同的每个子任务的 id。 I have no idea how to do this.我不知道该怎么做。 Please help.请帮忙。 I couldn't code anything for this.我无法为此编写任何代码。 I tried using layout inflater, searched up the net on what to do, but I still have no clue.我尝试使用布局充气器,在网上搜索了该怎么做,但我仍然不知道。 Please help.请帮忙。

You can user dialog fragment for this and is look effective also Let me give you link... dialog fragment您可以为此使用对话框片段,并且看起来也很有效让我给您链接...对话框片段

I have done something similar in my project, adding horizontal linear layouts in a vertical linear layout programmatically.我在我的项目中做了类似的事情,以编程方式在垂直线性布局中添加水平线性布局。

Please check the below code:请检查以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM