我有一个扩展RelativeLayout的类。在这里我有另一个相对布局,它有一定的高度和宽度,在里面我有一个webview和一个按钮。此按钮位于第二个相对布局的右上角。我将webview的位置设置在第二个相对布局的顶部,左侧,右侧,底部和中心,基于在开关案例中传递的枚举。我面临的问题是,当我将webview设置在顶部时,它与按钮重叠并且按钮不可见。 这是我的代码:
enum position {
Top, Bottom, Left, Right, Center
};
public class Sample extends RelativeLayout {
RelativeLayout layout = new RelativeLayout(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
width, height);
addView(layout, params);
closeButton = new Button(context);
closeButton.setId(1);
closeButton.setText("X");
RelativeLayout.LayoutParams child1Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
child1Params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
child1Params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(closeButton, child1Params);
closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
closePressed();
}
});
RelativeLayout.LayoutParams child2Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
switch (position) {
case Top:
webView = new WebView(context);
child2Params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(webView, child2Params);
break;
case Bottom:
webView = new WebView(context);
child2Params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(webView, child2Params);
break;
case Left:
webView = new WebView(context);
child2Params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layout.addView(webView, child2Params);
break;
case Right:
webView = new WebView(context);
child2Params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(webView, child2Params);
break;
case Center:
webView = new WebView(context);
child2Params.addRule(RelativeLayout.CENTER_VERTICAL);
layout.addView(webView, child2Params);
break;
default:
webView = new WebView(context);
child2Params.addRule(RelativeLayout.CENTER_VERTICAL);
layout.addView(webView, child2Params);
break;
}
}
请您告诉我如何在按钮下方设置webview,使其始终显示在按钮下方并且按钮不会隐藏在按钮下方? 谢谢。