开发者

Dynamic RelativeLayout in Android: Scroll not Working

I want to put a scroll in vertical orientation in a Relative Layout that i created programmatically. But my scroll do not work. Can anyone help me? Here is the code that i'm using:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.screen1); 
    for (i = 1; i < 20; i++) { 
        RelativeLayout.LayoutParams p = new 
        RelativeLayout.LayoutParams( 
                150, 
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );
        layout.setScrollContainer(true);
     开发者_运维百科   ScrollView vscroll = new ScrollView(this);
        vscroll.setFillViewport(true);
        layout.setVerticalScrollBarEnabled(true);
        layout.addView(vscroll);
        p.addRule(RelativeLayout.BELOW, i-1);
        p.addRule(RelativeLayout.CENTER_HORIZONTAL);
        Button buttonView = new Button(this);
        buttonView.setId(i);
        buttonView.setText(i);
        buttonView.setLayoutParams(p);
        buttonView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
            Dialog(((Button)arg0).getId());
            } });
        layout.addView(buttonView, p);
    } 


I think you need to be adding your buttonViews to the ScrollView instead of the layout. ScrollView is a container View (like RelativeLayout). I think what your code is doing is adding a 0 height ScrollView to the top of your RelativeLayout, then a button after that. Since the button is not in the ScrollView, your 20 buttons won't scroll.


Try as below it may help

vscroll.addView(button);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜