创建自定义WebView

时间:2017-01-05 11:03:27

标签: android android-webview android-custom-view

我在程序的不同部分有很多WebView,但这些WebViews彼此没有区别,这就是我想用必要的设置创建自定义WebView的原因。目前,WebView没有显示,但没有错误。难道我做错了什么?

public class MyWebView extends WebView {

    MyWebView mMyWebView;

    public MyWebView(Context context) {
        super(context);
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

     public MyWebView initView(Context context){
         LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         MyWebView view = (MyWebView) inflater.inflate(R.layout.custom_webview, this);
         view.getSettings().setJavaScriptEnabled(true) ;
         view.getSettings().setUseWideViewPort(true);
         view.getSettings().setLoadWithOverviewMode(true);
         view.getSettings().setDomStorageEnabled(true);
        return view;
    }
}

custom_webview.xml

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

    <WebView
        android:id="@+id/custom_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

MainActivity:

public class MainActivity extends AppCompatActivity {

  MyWebView mWebView;

   @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = new MyWebView(MainActivity.this);
    mWebView = mWebView.initView(MainActivity.this);
    mWebView = (MyWebView) findViewById(R.id.web_view);

  }
  @Override
  protected void onResume() {
    mWebView.loadUrl("https://www.google.com/");
  }

}

main_activity.xml:

 <com.mypack.webview.MyWebView
        android:id="@+id/web_view"
        android:layout_below="@+id/start_URL"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

1 个答案:

答案 0 :(得分:0)

您应该重写“MyWebView”类。您的方法“initView”每次都会生成一个新的MyWebView对象,但它永远不会连接到您的MainActivity。这可能是你没有看到它的原因。

也许尝试类似(未经测试的)

    public class MyWebView extends WebView {

        public MyWebView(Context context) {
            super(context);
            initView(context);
        }

        public MyWebView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initView(context);

        }

        private void initView(Context context){
             // i am not sure with these inflater lines
             LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             // you should not use a new instance of MyWebView here             
             // MyWebView view = (MyWebView) inflater.inflate(R.layout.custom_webview, this);
             this.getSettings().setJavaScriptEnabled(true) ;
             this.getSettings().setUseWideViewPort(true);
             this.getSettings().setLoadWithOverviewMode(true);
             this.getSettings().setDomStorageEnabled(true);

        }
    }

并用

调用它
public class MainActivity extends AppCompatActivity {

      MyWebView mWebView;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (MyWebView) findViewById(R.id.web_view);

      }
      @Override
      protected void onResume() {
        mWebView.loadUrl("https://www.google.com/");
      }

}