WebView設置透明,滾動時有黑影,加載圖片

解決黑影的關鍵代碼: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);

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章