how to adjust the webview width relating to screen size in android? need a hack
Im using a customized webview as follows
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:MyWebView="http://schemas.android.com/apk/res/com.mds.android.mireader.webview" android:background="#FFFFFF" android:layout_height="fill_pare开发者_如何学Gont" android:layout_width="fill_parent">
<com.mds.android.mireader.webview.MyWebView
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/myweb" **android:layout_width="318px"** android:layout_height="fill_parent" > </com.mds.android.mireader.webview.MyWebView>
im doing some thing strange, for that i need to satisfy following condition
condition: webview width should always divisible by 3
So i can't give fixed value or fill_parent in above xml file.For example if i give fill_parent it works fine for portrait(assume HVGA emulator with portrait width=480 which is divisible by 3)but no success for landscape(bcz landscape width 800 which is not divisible by 3).
Solution should work for both orientation change and device change"
Im struggling with 1 or 2 pixel values difference and it will be solved if i satisfy that condition. TIA ....
HI,
You can get the height and width of the screen using following piece of code:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Now you have the width you can check whether it is divisible by 3 or not. If yes then well and good, otherwise you can subtract the modulus(width%3) from the original width and set it as the width of your webview.
Add A View Programmatically:
Sample XML:
<LinearLayout (define id:testLayout, width, height here> </LinearLayout>
Java Code:
LinearLayout linLayout = (LinearLayout) findViewById(R.id.testlayout);
// create an object of your webview and set all the properties you want like background color etc.
MyWebView myWebView = new MyWebView();
linLayout.addView(myWebView, new LinearLayout.LayoutParams(<my calculated width>, LayoutParams.FILL_PARENT);
PS: you dont have to add the webview object in your XML file. "addView" function will add your webview inside your linear layout object.
Hope this helps!!
精彩评论