Reputation: 3147
I am creating my controls programmatically, based on JSON received from a server. One of the controls I need to create is a WebView
that is horizontally centred on the screen. This is simple in xml layouts as shown below using the layout_gravity option. But how do you do this in code, the WebView
unlike TextView
does not have a setGravity(Gravity.HORIZONTAL_GRAVITY_MASK)
method.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
Upvotes: 0
Views: 4277
Reputation: 6824
I would use a RelativeLayout, then you can use LayoutParams when adding the view:
RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
relativeLayout.addView(yourWebView, layoutParams);
Upvotes: 2
Reputation: 6201
i use LinearLayout to show webview and setGravity.
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
linearLayout.setGravity(Gravity.CENTER);
WebView view = new WebView(this);
linearLayout.addView(view);
setContentView(linearLayout);
it help you.
Upvotes: 0