LayoutParams: how to put button at bottom
I'm trying to put a button at the bottom of a webview. For some reason, it keeps showing up at the top. Here's my code:
webview = new WebView(this);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
Button myButton = new Button(this);
myButton.setId(11);
LayoutParams buttonParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
myButton.setText("A Button!");
myButton.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, -1);
myButton.setLayoutParams(buttonParams);
webview.setLayoutParams(params2);
//setBackgroundcolor(0) - You must make the background color transparent before setting backgroundResource
webview.setBackgroundColor(0);
webview.setBackgroundResource(R.drawable.background);
webview.addView(myButton);
Edit --
I'm gonna go ahead and set up the base of my reusable view in xml. I'll just use ScrollView
with a RelativeLayo开发者_如何学Pythonut
inside of it (containing my WebView
and Button
). The reason is because I only want the Button
to show up once I reach the end of the webpage.
WebView
extends AbsoluteLayout
, so you can't use RelativeLayout.LayoutParams
. Moreover, AbsoluteLayout
is deprecated, and WebView
is definitely not a view to be used as a container.
You can overlay a RelativeLayout
over the WebView
putting them in a FrameLayout
, they'll get piled up, with the last view added (your RelativeLayout) at the top.
WebView
extends AbsoluteLayout
, not RelativeLayout
, so adding a rule for a RelativeLayout
will have no affect. Try setting the layout gravity of the button to bottom.
buttonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
精彩评论