2017-01-05 2 views
3

프로그램의 다른 부분에 WebView가 많이 있지만 이러한 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> 

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

로 전화하면 layout_below = "@ 아이디/START_URL", 어쩌면이 하나가 높이 match_parent는 말? –

+0

내가 MainActivity에 버튼이, 아니 생각 <버튼 안드로이드 : layout_width = "wrap_content는" 안드로이드 : layout_height = "wrap_content" 안드로이드 : 텍스트 = 안드로이드 "START"ID를 = "@ + ID/START_URL"/> – Delphian

+0

layout_below 속성을 제거해 볼 수 있습니까? 그러면 어떻게됩니까? –

답변

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/"); 
     } 

} 
+1

감사합니다. mWebView = (MyWebView) findViewById (R.id.web_view)와 같은 행을 사용할 때 작동합니다. mWebView = new MyWebView (MainActivity.this)가 작동하지 않습니다! – Delphian

+0

수락 해 주셔서 감사합니다. 곧 코드를 다시 업데이트하겠습니다. –