简体   繁体   English

无法设置Android Webview宽度

[英]Unable to set Android webview width

I have been trying to add a Webview control to my Android app on a screen using TableLayout Unfortunately whatever i try, the webview always covers the whole width of the screen The layout file contains the following code 我一直在尝试使用TableLayout将Webview控件添加到屏幕上的Android应用中不幸的是,无论我如何尝试,Webview始终覆盖屏幕的整个宽度。布局文件包含以下代码

<TableRow android:layout_gravity="center">

    <WebView
        android:id="@+id/someid"
        android:layout_width="wrap_content" 
        android:layout_height="100dp">
    </WebView>
</TableRow>

The Activity file contains the following 活动文件包含以下内容

@Override protected void onFinishInflate(){

    WebSettings webSettings = mywebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
    mywebView.setScrollContainer(false);
    mywebView.addJavascriptInterface(new WebAppInterface(context), "Android");

}

Is there any specific reason why the control would always cover the whole screen width. 是否有任何特定原因导致控件始终覆盖整个屏幕宽度。 The height attribute seems to work but whatever I use for android:layout_width the width is always 100% of screen width. height属性似乎可以正常工作,但是无论我用什么android:layout_width ,宽度始终是屏幕宽度的100%。 I have tried giving a fixed value to android:layout_width as well as multiple combinations of the websettings in the java file. 我尝试给android:layout_width以及Java文件中的websettings的多个组合赋予固定值。

Any clues why this could be happening? 任何线索为什么会发生这种情况?

You have to define at first the parameters of your tableRow . 首先必须定义tableRow的参数。 For example: layout_width and layout_height inside tableRow : 例如: tableRow内的layout_width和layout_height:

<TableRow android:layout_gravity="center"
          android:layout_width="wrap_content"
          android:layout_height="100dp">
<WebView
    android:id="@+id/someid"
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent">
</WebView>

Strangely, wrappping the control in a LinearLayout fixed the issue 奇怪的是,将控件包装在LinearLayout中解决了该问题

  <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_gravity="center"
  android:layout_width="wrap_content"
  android:orientation="vertical" 
  android:layout_height="wrap_content"> 
     <WebView
        android:id="@+id/someid"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:layout_width="250px"
         android:orientation="vertical" 
        >
     </WebView>
  </LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM