如何在Android中以编程方式使按钮滚动?

时间:2013-12-02 05:58:30

标签: java android button scrollview

我试图在屏幕上创建任意数量的按钮,然后有一种方法来滚动按钮。 (上/下)

通过一些研究,我发现我应该使用“scrollview”来做我正在做的事情。所以我把它设置成这样......(http://pastebin.com/05mtszi7处的完整代码)

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); 

LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,      LayoutParams.WRAP_CONTENT));

row.addView(btnTag);

ScrollView sc = new ScrollView(this);
sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
sc.setFillViewport(true);
sc.addView(row);

layout.addView(sc);

你知道我做错了什么吗?我知道,这可能是非常简单/愚蠢的事情。我看了一遍,但我必须在这里忽略一些重要的东西。一切都运行良好,但没有任何滚动。

在此先感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

你应该添加按钮到布局然后布局添加到scrollview.scrollview只能有一个孩子。

         LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
                layout.setOrientation(LinearLayout.VERTICAL); 

                TextView menuText = new TextView(this);
                menuText.setText("People");
                menuText.setId(100);
                menuText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                layout.addView(menuText);

                for (int i = 0; i < users_registered; i++) {

                    LinearLayout row = new LinearLayout(this);
                    row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));


                        Button btnTag = new Button(this);
                        btnTag.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                        btnTag.setText(user_name[i] + " " + i);
                        btnTag.setId(1 + (i));
                        row.addView(btnTag);

                        btnTag.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                String buttonText = "Unknown Person";
                                    Button b = (Button) findViewById(v.getId());
                                    buttonText = (String) b.getText();

                                TextView tx = (TextView) findViewById(100);
                                tx.setText(buttonText + " pressed.");
                            }
                        });
    layout.addView(row); 
        }//end loop

                    ScrollView sc = new ScrollView(this);
                    sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                    sc.setFillViewport(true);

                    sc.addView(layout);

                   // layout.addView(sc); 

答案 1 :(得分:0)

试试这段代码:

    ScrollView sc = new ScrollView(this);
    sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    sc.setFillViewport(true);

    LinearLayout layout = new LinearLayout(this);
    layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    layout.setOrientation(LinearLayout.VERTICAL); 

    LinearLayout row = new LinearLayout(this);
    row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    Button btnTag = new Button(this);
    btnTag.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

    row.addView(btnTag);

    layout.addView(row);
    sc.addView(layout);

这会有所帮助......

答案 2 :(得分:0)

使用此代码。这会对你有帮助......

 LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL); 

        TextView menuText = new TextView(this);
        menuText.setText("People");
        menuText.setId(100);
        menuText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        layout.addView(menuText);

        for (int i = 0; i < 10; i++) {

            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


                Button btnTag = new Button(this);
                btnTag.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                btnTag.setText( " " + i);
                btnTag.setId(1 + (i));
                row.addView(btnTag);

                btnTag.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        String buttonText = "Unknown Person";
                            Button b = (Button) findViewById(v.getId());
                            buttonText = (String) b.getText();

                        TextView tx = (TextView) findViewById(100);
                        tx.setText(buttonText + " pressed.");
                    }
                });


            ScrollView sc = new ScrollView(this);
            sc.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            sc.setFillViewport(true);

            sc.addView(row);

            layout.addView(sc);

            setContentView(layout);