【问题标题】:How to set border for Android webview如何为Android webview设置边框
【发布时间】:2011-08-24 09:49:34
【问题描述】:

在我的程序中,当按钮单击时,webview 会以单独的布局加载。该布局只有该网络视图。我想为此添加边框。我将单独的 XML 如下添加到该 web 视图的背景中,但 不起作用。

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="10dp" android:color="#000000" />
<padding android:left="2dp" android:top="2dp" android:right="2dp"android:bottom="2dp"/>
</shape>

如何在 Android 中为 webview 添加边框..? 谢谢

【问题讨论】:

标签: android android-webview


【解决方案1】:

将 WebView 封装在一个 Layout 中,给 Layout 添加边框,并在布局中保持 2dp 的 padding。

【讨论】:

  • 需要填充的任何具体原因?
  • 可能有点晚了,但是 padding 就像对象内部的边距,所以需要 padding 来防止 webview 重叠到布局背景中
【解决方案2】:

Abhinav 的答案是正确的,我只是为像我这样遇到此答案并且甚至不知道如何“将 WebView 包含在布局中”或“将边框添加到布局”;希望它可以帮助某人:

  1. /res 下创建一个新目录并将其命名为drawable(您已经拥有drawable-hdpidrawable-mdpi 等;这些用于不同的分辨率——无论如何都将使用这个新目录drawable分辨率)。
  2. drawable下新建一个XML文件,命名为border.xml(在Android Studio中,可以右键drawable目录,点击New>Drawable Resource File)。
  3. 将 miuranga 的 XML 的全部内容粘贴到 border.xml 并保存。这称为“可绘制资源”,将在下一步中拉入您的布局文件中。
  4. 在您的布局文件中,围绕 WebView 创建一个新布局,如下所示,我对子 LinearLayout 进行了操作。请注意,LinearLayout 检索属性android:background 设置为@drawable/border 的可绘制资源。我相信它是通过文件名减去扩展名来检索border.xml。通过将边框添加到包围 WebView 的 Layout,您可以在视觉上实现 Webview 周围的边框,效果很好。

activity_main.xml 内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.test.app.MainActivity"
   android:orientation="vertical">
<LinearLayout android:background="@drawable/border"
                  android:layout_width="match_parent"
                  android:layout_height="380px">
    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
                        android:id="@+id/webview"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" />
     </LinearLayout>
</LinearLayout>

【讨论】:

  • 感谢这对我很有用。要添加的另一件事:您可以将 android:padding="5dp" 添加到根 LinearLayout (第一个)中,以便为边框从屏幕边缘留出一些空间。
【解决方案3】:

WebView 本身不能将 Drawable 作为背景 - 请参阅 WebView.java 中的 WebView.onDraw。 它只有纯色,默认或取自 html 内容。 解决方案是(如前所述)将 WebView 作为其他 Widget 的子级。

【讨论】:

    猜你喜欢
    • 2018-12-07
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    相关资源
    最近更新 更多