简体   繁体   English

以编程方式创建带有 2 列按钮的滚动视图 Android Studio Java

[英]Programmatically create a scrollview with 2 columns of buttons Android Studio Java

I have a scrollview created that I add a variable amount of buttons to programmatically, but I now have to make it so that I have a second smaller button beside each of the buttons within the scrollview.我创建了一个滚动视图,我以编程方式添加了可变数量的按钮,但我现在必须这样做,以便在滚动视图中的每个按钮旁边都有第二个较小的按钮。 Here is my screen now:这是我现在的屏幕: ![这是我现在的滚动视图

I would like there to be a small button beside each of those that would let me delete the option or something along those lines.我希望每个按钮旁边都有一个小按钮,可以让我删除选项或类似的东西。

Here is my code I have now to build the buttons这是我现在必须构建按钮的代码

    public void addButtons() {
        LinearLayout linearLayout = findViewById(R.id.loadLinearLayout);

        int i = 0;
        for(File file : files) {
            Button newButton = new Button(this);
            String filename = file.getName();
            newButton.setId(i);
            newButton.setText(filename);
            newButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    loadGame(view.getId());
                }
            });
            i++;

            linearLayout.addView(newButton);
        }
    }

Any help would be greatly appreciated.任何帮助将不胜感激。

Instead of adding a Button for each file, add a whole LinearLayout for each of them, with horizontal orientation.不要为每个文件添加一个Button ,而是为每个文件添加一个完整的LinearLayout ,并具有水平方向。 Then, add to this inner linear layout first the button with the file name, then the delete button (they will appear in a row).然后,首先将带有文件名的按钮添加到此内部线性布局,然后是删除按钮(它们将连续出现)。

public void addButtons() {
    LinearLayout linearLayout = findViewById(R.id.loadLinearLayout);

    int i = 0;
    for(File file : files) {
        //create horizontal linear layout
        LinearLayout innerLayout = new LinearLayout(this);
        innerLayout.setOrientation(LinearLayout.HORIZONTAL);

        Button newButton = new Button(this); //button which shows file name
        String filename = file.getName();
        newButton.setId(i);
        newButton.setText(filename);
        newButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadGame(view.getId());
            }
        });
        innerLayout.addView(newButton);

        Button deleteButton = new Button(this);
        deleteButton.setText("Delete"); //use a string resource ideally
        innerLayout.addView(deleteButton);

        linearLayout.addView(innerLayout); //add the inner layout to the outer
        i++;
    }
}

You might need to do some additional styling, such as set the distance between the buttons, or align them in a way.您可能需要做一些额外的样式设置,例如设置按钮之间的距离,或者以某种方式对齐它们。 You should also be able to do that programmatically somehow, but that is a different story.你也应该能够以某种方式以编程方式做到这一点,但那是另一回事。

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

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