如何调整与屏幕尺寸在Android中的WebView宽度是多少?需要一个黑客 [英] how to adjust the webview width relating to screen size in android? need a hack

查看:168
本文介绍了如何调整与屏幕尺寸在Android中的WebView宽度是多少?需要一个黑客的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用自定义的WebView如下:

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_parent" 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

状态:的WebView宽度应始终整除3

所以我不能给上面的xml file.For例如固定值或FILL_PARENT,如果我给FILL_PARENT它工作正常进行纵向(承担与纵向宽度HVGA模拟器= 480这是由3整除),但对景观没有成功( BCZ景观宽度800这是不被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"

林有1或2个像素值挣扎差异,如果我满足该条件将得到解决。 TIA ....

Im struggling with 1 or 2 pixel values difference and it will be solved if i satisfy that condition. TIA ....

推荐答案

您好,

您可以用下面的一段code获取屏幕的高度和宽度:

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;

现在你可以检查它是否是由3或不能整除的宽度。如果是的话当然好,否则你可以从原来的宽​​度减去模量(宽度%3),将其设置为你的WebView的宽度。

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.

添加视图以编程方式:

示例XML:

<LinearLayout (define id:testLayout, width, height here> </LinearLayout>

Java的code:

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:你没有添加web视图对象在XML文件中。 addView功能将添加您的WebView的线性布局对象中。

PS: you dont have to add the webview object in your XML file. "addView" function will add your webview inside your linear layout object.

希望这有助于!

这篇关于如何调整与屏幕尺寸在Android中的WebView宽度是多少?需要一个黑客的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆