Android 中的动态相对布局:滚动不起作用
我想在我以编程方式创建的相对布局中以垂直方向放置滚动条。但我的卷轴不起作用。谁能帮助我吗? 这是我正在使用的代码:
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 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要将 ButtonView 添加到 ScrollView 而不是布局中。 ScrollView 是一个容器视图(如RelativeLayout)。我认为您的代码正在做的是将 0 高度的 ScrollView 添加到您的relativelayout 的顶部,然后在其后添加一个按钮。由于该按钮不在 ScrollView 中,因此您的 20 个按钮不会滚动。
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