我正在尝试使用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);
然而,唯一出现的是scrollView。我猜我在这里遗漏了一些小东西...我读了文档并且我相信我这样做是正确的但事实上它不起作用表明我做错了或者无法完成我想做的事。
我想要这样看的视觉如下:
答案 0 :(得分:1)
您需要在最后调用setContentView(layout1)
来实际添加布局。
编辑:
好的,所以我修复了你的代码,它正在我的手机上工作。但是我无法与DrawView
明显合作,所以如果出现问题我无法帮助你。放手一搏。
RelativeLayout.LayoutParams exitparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
exitparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams zimparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams zoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams scrollparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
zimparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// Exit button
Button exitbutton = new Button(this);
exitbutton.setText("Exit");
exitbutton.setGravity(Gravity.CENTER);
exitbutton.setWidth(200);
exitbutton.setLayoutParams(exitparams);
exitbutton.setId(2);
// Zoom In button
Button zoomOutButton = new Button(this);
zoomOutButton.setText("Zoom Out");
zoomOutButton.setGravity(Gravity.CENTER);
zoomOutButton.setWidth(200);
zoomOutButton.setLayoutParams(zimparams);
zoomOutButton.setId(1);
// Zoom In button
Button zoomInButton = new Button(this);
zoomInButton.setText("Zoom In");
zoomInButton.setGravity(Gravity.CENTER);
zoomInButton.setWidth(200);
zoutparams.addRule(RelativeLayout.LEFT_OF, 1);
zoutparams.addRule(RelativeLayout.RIGHT_OF, 2);
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);
scrollparams.addRule(RelativeLayout.BELOW,1);
scrollView.setLayoutParams(scrollparams);
layout1.addView(scrollView);
setContentView(layout1);