Android webview设置边框的方法
在我的程序中,单击按钮时 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 中为 Web 视图添加边框..? 谢谢
In my programme webview is load in separate layout when button click. that layout only have that web view. I want to add border for that. I add separate XML as follows to background for that webview but is not work.
<?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>
how can I add a border for webview in Android..?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 WebView 封装在布局中,向布局添加边框,并在布局中保留 2dp 的内边距。
Enclose the WebView in a Layout, add the border to the Layout, and keep a padding of 2dp in the layout.
Abhinav 的答案是正确的,我只是为像我这样的绝对初学者添加这个附加信息,他们遇到这个答案,甚至还不知道如何“将 WebView 包含在布局中”或“将边框添加到布局中”;希望它可以帮助某人:
/res
下创建一个新目录并将其命名为drawable
(您已经拥有drawable-hdpi
、drawable-mdpi
等;这些用于不同的分辨率 - 无论分辨率如何,都会使用这个新目录drawable
)。drawable
下创建一个新的XML文件,并将其命名为border.xml
(在Android Studio中,您可以右键单击drawable
目录,然后单击新建>可绘制资源文件)。border.xml
中并保存。这称为“可绘制资源”,将在下一步中拉入您的布局文件中。android:background
设置为@drawable/border
。我相信它是通过文件名减去扩展名来检索 border.xml 的。通过将边框添加到包围 WebView 的布局中,您可以在视觉上实现 Webview 周围的边框,效果很好。Activity_main.xml 内容:
Abhinav's answer is right on, I'm just adding this additional info for absolute beginners like myself who encounter this answer and don't even know yet how to "enclose the WebView in a Layout" or "add the border to the Layout"; hopefully it can help someone:
/res
and name itdrawable
(you'll already havedrawable-hdpi
,drawable-mdpi
, etc; these are for different resolutions--this new directorydrawable
will be used regardless of resolution).drawable
and name itborder.xml
(in Android Studio, you can right-click on thedrawable
directory and click New>Drawable Resource File).border.xml
and save it. This is called a "Drawable Resource" and will be pulled into your layout file in the next step.android:background
set to@drawable/border
. I believe it's retrievingborder.xml
by its filename minus the extension. By adding the border to the Layout that encloses the WebView, you're visually achieving a border around the Webview, which works nicely.activity_main.xml contents:
WebView 本身不能将 Drawable 作为背景 - 请参阅 WebView.java 中的 WebView.onDraw。
它只有纯色、默认颜色或取自 html 内容。
解决方案是(正如已经建议的那样)将 WebView 作为其他 Widget 的子级。
WebView itself can't have Drawable as background - see WebView.onDraw in WebView.java.
It has solid color only, default or taken from html content.
Solution is (as already suggested) to make WebView as child of other Widget.