目前我正在使用以下设置高度和宽度:
//landsapeWebView is a WebView, and deviceHeight and deviceHeight are ints
landsapeWebView.setLayoutParams(new RelativeLayout.LayoutParams(deviceHeight, deviceWidth));
这似乎在某些设备上运行良好,但在其他设备上却没有。我得出的唯一结论是,这样设置高度和宽度:
<WebView android:layout_width="(deviceWidth)dp"
android:layout_height="(deviceHeight)dp" />
当我想要的是这个:
<WebView android:layout_width="(deviceWidth)px"
android:layout_height="(deviceHeight)px" />
如何指定计量单位?
@collusionbdbh的额外披露
display = getWindowManager().getDefaultDisplay();
if(getResources().getConfiguration().orientation == 1){
deviceWidth = display.getWidth();
deviceHeight = display.getHeight();
}else{
deviceWidth = display.getHeight();
deviceHeight = display.getWidth();
}
答案 0 :(得分:2)
试试这个:
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
答案 1 :(得分:0)
你确定这不是一个方向问题吗?
无论设备的方向如何,宽度都是水平的,高度是垂直的。
我会试试这个:
display = getWindowManager().getDefaultDisplay();
deviceWidth = display.getWidth();
deviceHeight = display.getHeight();
我很确定宽度应该在高度之前:
landsapeWebView.setLayoutParams(new RelativeLayout.LayoutParams(deviceHeight, deviceWidth));
应该是:
landsapeWebView.setLayoutParams(new RelativeLayout.LayoutParams(deviceWidth, deviceHeight));