【问题标题】:CSS border radius not working into Android webviewCSS边框半径不适用于Android webview
【发布时间】:2014-03-12 02:59:01
【问题描述】:

我有一个基于 Webview 的 Android 应用。

应用程序的主要部分是使用 JS、HTML 和 CSS 设计的。它也基于 Bootstrap。

在全球范围内,该应用运行良好。但是今天让我担心的问题是:一个按钮没有正确显示,但这个问题只在今天的一款智能手机型号上出现(Android 4.2.2 下的阿尔卡特 TCT Orange Hiro)。

按钮是用 CSS 绘制的圆圈设计的。智能手机上方的圆圈显示为正方形,好像 Webview 既不理解border-radius 也不理解-webkit-border-radius CSS 命令。

HTML:

<div class="row-fluid">
        <div class="text-center">
            <div id="button_border" class="button_play">                    
                <div id="button_content" class="button_play">
                    <div id="button_content_play" class="pictogramme">&#9654;</div>
                </div>
            </div>
        </div>
</div>

编辑:CSS 是通过以下方式在 HTML 中加载的:

<link rel="stylesheet" type="text/css" href="css/main.css" />

为此,CSS 包含:

/*position and global appearance of the button's outer portion. The circle is drawn by using border-radius: 50% */
#button_border {
    display: block;
    position: absolute;
    top: 27%;
    left: 20%;
    width: 55%;
    z-index: 7;
    -webkit-border-radius: 50%;
    border-radius: 50%;
}
/*Specific design for the play button's outer portion*/
#button_border.button_play {
    background-color: #01b169;
    border: solid .5em #3c3e43;
}
/*Button colors change when touched*/
#button_border.button_play:focus, 
#button_border.button_play:hover {
    color: white;
    background-color: white;
}

/*Position and global appearance of the button's body portion. The circle is drawn by using border-radius: 50% */
#button_content {
    position: absolute;
    z-index: 8;
    -webkit-border-radius: 50%;
    border-radius: 50%;
}
#button_content.button_play {
    background-color: white;
    color: #01b169;
    border: solid .1em #01b169;
}
/*Body button colors change when touched*/
#button_content.button_play:focus, 
#button_content.button_play:hover {
    color: white;
    background-color: #01b169;
}
/*Play symbol has to be centered in the button*/
#button_content_play {
    text-align: center;
}

编辑:加载 web 视图的 java 代码如下:在我的 MainActivity 的 onCreate() 方法中,我有:

    webView = (WebView) findViewById(R.id.webView);

    WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDatabaseEnabled(true);
    settings.setDomStorageEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);

    // Add HTML views to the webviews
    webView.loadUrl("file:///android_asset/www/home.html");

    /**
     * Allow access from file URL (for AJAX request and
     * internationalization)
     */
    if (Build.VERSION.SDK_INT >= 16) {
        Class<?> clazz = webView.getSettings().getClass();
        Method method;
        try {
            method = clazz.getMethod("setAllowUniversalAccessFromFileURLs",
                    boolean.class);
            if (method != null) {
                method.invoke(webView.getSettings(), true);
            }
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

有人知道我该如何解决这个问题吗?

注意:我遇到的其他困难是我自己无法重现该问题,因为我不拥有此特定设备,并且 Android 4.2.2 的模拟器按预期显示按钮。

提前谢谢你救了我的命

【问题讨论】:

  • 告诉我你的代码如何将css加载到webview
  • 我编辑了我的问题,添加了加载 web 视图的 java 代码和链接 CSS 文件的 html 代码

标签: android html android-webview css


【解决方案1】:

请替换这个css.....

#button_content {

     position: absolute;
     z-index: 8;

     border-radius: 50px;
    -o-border-radius topleft: 50px;  
    -o-border-radius topright: 50px;
    -o-border-radius bottomleft: 50px;
    -o-border-radius bottomright: 50px;

    -webkit-border-radius topleft: 50px;  
    -webkit-border-radius topright: 50px;
    -webkit-border-radius bottomleft: 50px;
    -webkit-border-radius bottomright: 50px;


}

【讨论】:

  • 我不太明白... 分隔border-radius 命令究竟有什么好处?此外,用 px 替换 % 会导致绘制圆角的正方形而不是我希望的圆形。关于您的回复的另一个问题是为什么在 Android 上使用 -o 前缀?是 Opera 浏览器的,不是吗?如果我没记错的话,Android webview 引擎是基于 Webkit 的。
  • 我很抱歉,但由于现代 Android 浏览器中奇怪的 CSS 行为,爆炸压缩的边界半径属性似乎可以解决问题。见stackoverflow.com/questions/17186158/…
  • 我将看到用户收到此问题,以向我确认是否已根据您的建议解决。我会通知你的。
  • 我刚从用户那里回来,爆炸压缩的border-radius属性并不能解决问题。不幸的是,什么都没有改变。我正在寻找任何其他建议。
【解决方案2】:

对我来说,如果不是所有的值都相等,它似乎可以工作。所以这失败了:

border-top-left-radius: 50px;
border-top-right-radius: 50px;
border-bottom-left-radius: 50px;
border-bottom-right-radius: 50px;

但以下工作:

border-top-left-radius: 50px;
border-top-right-radius: 50px;
border-bottom-left-radius: 50px;
border-bottom-right-radius: 49.99999px;

不要添加太多小数,因为 49.999999px 不起作用。

【讨论】:

    【解决方案3】:

    当圆角元素也有背景时会发生这种情况,从圆角泄漏。

    这对我有用:

    -webkit-background-clip: padding-box;
    -moz-background-clip: padding-box;
    background-clip: padding-box;
    

    【讨论】:

      猜你喜欢
      • 2018-05-07
      • 2011-03-21
      • 2011-10-25
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      相关资源
      最近更新 更多