2010-11-11 5 views
3

xmlAndroid: Comment afficher Webview dans Tabview?

<?xml version="1.0" encoding="utf-8"?> 
<WebView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/myWebView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" /> 

java

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.webview); 
    WebView webView = (WebView) findViewById(R.id.myWebView); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl("http://www.google.com"); 
} 

TabView xml

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:padding="5dp"> 
     <TabWidget android:id="@android:id/tabs" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" 
      android:padding="5dp" /> 
    </LinearLayout> 
</TabHost> 

TabView java

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabview); 

     Resources res = getResources(); // Resource object to get Drawables 
     TabHost tabHost = getTabHost(); // The activity TabHost 
     TabHost.TabSpec spec; // Resusable TabSpec for each tab 
     Intent intent; // Reusable Intent for each tab 


     intent = new Intent().setClass(this, WebActivity.class); 
     spec = tabHost.newTabSpec("webview") 
       .setIndicator("webview", res.getDrawable(R.drawable.info)) 
       .setContent(intent); 
     tabHost.addTab(spec); 

       // add other tabs 

     tabHost.setCurrentTab(0); 
    } 

Cela lancera un webView en plein écran.
Est-il possible d'afficher webview à l'intérieur du tabview?

Répondre

2

Vous pouvez trouver la réponse ici: http://developer.android.com/guide/webapps/webview.html#HandlingNavigation

Par WebView par défaut à chaque fois que lance le navigateur que vous visitez une nouvelle URL, donc ce qui se passe est, il lance le navigateur.

Pour éviter cela chaque fois que vous cliquez sur quelque chose etc, vous devez ajouter un WebViewClient à votre WebView:

WebView myWebView = (WebView) findViewById(R.id.webview); 
myWebView.setWebViewClient(new WebViewClient()); 
myWebView.loadUrl("http://www.example.com"); 

Si vous devez peform une action particulière lorsqu'un utilisateur clique sur un lien, mettre en œuvre votre propre WebViewClient :

public class MyWebViewClient extends WebViewClient { 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     boolean result = false; 

     /* ... */ 
     // Return false to proceed loading page, true to interrupt loading 

     return result; 
    } 
} 

et de l'utiliser:

myWebView.setWebViewClient(new MyWebViewClient());