【问题标题】:Create a custom WebView创建自定义 WebView
【发布时间】:2017-01-05 11:03:26
【问题描述】:

我在程序的不同部分有很多 WebView,但这些 WebView 彼此之间没有区别,这就是为什么我想创建一个具有必要设置的自定义 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>

主活动:

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"/>

【问题讨论】:

  • 你说layout_below="@id/start_URL",也许这个是身高match_parent?
  • 我认为没有,我在 MainActivity 中有按钮
  • 您可以尝试删除 layout_below 属性,然后会发生什么?
  • 当我将设置放入 MainActivity 时一切正常。我认为一个与继承有关的问题。

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


【解决方案1】:

您可能应该重写您的“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/");
      }

}

【讨论】:

  • 谢谢!当我使用以下行时它可以工作:mWebView = (MyWebView) findViewById(R.id.web_view),但使用以下行:mWebView = new MyWebView(MainActivity.this) 不起作用!
  • 感谢您的采纳,我会尽快再次更新代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-13
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 2013-08-29
相关资源
最近更新 更多