您的位置:首页 > 移动开发

WebView设置透明,滚动时有黑影,加载图片

2017-08-01 11:10 423 查看
解决黑影的关键代码:android:layerType=”software”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/p_bg"
android:orientation="vertical">

<include layout="@layout/common_header"/>

<TextView
android:id="@+id/tv_news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:gravity="center"
android:layout_margin="10dp"
android:textColor="@android:color/white"
android:visibility="gone"
android:textSize="25sp"
/>

<WebView
android:id="@+id/wv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layerType="software"
/>

</LinearLayout>


展示数据与透明

public class HtmlActivity extends BaseActivity
{
public static final String EXTRA_CONTENT = "extra_content";
public static final String EXTRA_TITLE = "extra_title";

public final static String CSS_STYLE ="<style>* {color:#fff} </style>";

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_html);
mWebView = findViewById(R.id.wv);
((TextView)findViewById(R.id.tv_news_title)).setText(getIntent().getStringExtra(EXTRA_TITLE));
WebSettings settings =  mWebView.getSettings();
//        settings.setJavaScriptEnabled(true);
settings.setBlockNetworkImage(false);//不阻塞网络图片
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
//5.0以后 https 图片地址,加这个
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
mWebView.loadDataWithBaseURL(null,CSS_STYLE+getIntent().getStringExtra(EXTRA_CONTENT), "text/html", "utf-8",null);
mWebView.setBackgroundColor(Color.TRANSPARENT);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息