我正在尝试使用RelativeLayout 来生成两行项目,一排三个按钮在上面,下面有一个滚动视图。我希望 scrollView 尽可能多地占据空间。
我相信 RelativeLayout 是这里最好的方法。我试图在运行时而不是通过 xml 布局文件来执行此操作。我的代码如下:
RelativeLayout.LayoutParams exitparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
exitparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams zimparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams zoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams scrollparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
zimparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
scrollparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM):
// Exit button
Button exitbutton = new Button(this);
exitbutton.setText("Exit");
exitbutton.setGravity(Gravity.CENTER);
exitbutton.setWidth(200);
exitbutton.setLayoutParams(exitparams);
// Zoom In button
Button zoomOutButton = new Button(this);
zoomOutButton.setText("Zoom Out");
zoomOutButton.setGravity(Gravity.CENTER);
zoomOutButton.setWidth(200);
zoomOutButton.setLayoutParams(zimparams);
// Zoom In button
Button zoomInButton = new Button(this);
zoomInButton.setText("Zoom In");
zoomInButton.setGravity(Gravity.CENTER);
zoomInButton.setWidth(200);
zoutparams.addRule(RelativeLayout.LEFT_OF, zoomInButton.getId());
zoomInButton.setLayoutParams(zoutparams);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
layout1.addView(exitbutton);
layout1.addView(zoomInButton);
layout1.addView(zoomOutButton);
exitbutton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
zoomInButton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
increaseZoom();
onResume();
}
});
zoomOutButton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
reduceZoom();
onResume();
}
});
drawView = new DrawView(this, height, width, SMD, zoomLevel);
drawView.setBackgroundColor(Color.WHITE);
ScrollView scrollView = new ScrollView(this);
scrollView.addView(drawView);
scrollView.setLayoutParams(scrollparams);
layout1.addView(scrollView);
setContentView(layout1);
然而,唯一出现的是滚动视图。我猜我在这里遗漏了一些小东西......我阅读了文档,我相信我这样做是正确的,但它不起作用的事实表明我要么做错了什么,要么无法完成我想做的事情。
我希望它看起来如何的视觉效果如下: