2013-03-22 68 views
1

我需要此代碼的幫助,我需要添加webview代碼,但他無法運行,我嘗試更改主要活動和fragmenttab1,但沒有任何結果。如何將WebView放置在帶有操作欄的Tab中Android

我使用操作欄,這對我來說很困難,因爲我對這種方法很陌生。

任何知道如何?

主要活動的代碼是這樣的:

public class MainActivity extends Activity { 

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

    //Obtenemos una referencia a la actionbar 
    ActionBar abar = getActionBar(); 

     //Establecemos el modo de navegación por pestañas 
    abar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    //Ocultamos el título de la actividad 
    //abar.setDisplayShowTitleEnabled(false); 

    //Creamos las pestañas 
    ActionBar.Tab tab1 = abar.newTab().setText("Tab1"); 
     ActionBar.Tab tab2 = abar.newTab().setText("Map"); 

     //Creamos los fragments de cada pestaña 
     Fragment tab1frag = new Tab1Fragment(); 
     Fragment tab2frag = new Tab2Fragment(); 

     //Asociamos los listener a las pestañas 
     tab1.setTabListener(new MiTabListener(tab1frag)); 
     tab2.setTabListener(new MiTabListener(tab2frag)); 

     //Añadimos las pestañas a la action bar 
     abar.addTab(tab1); 
     abar.addTab(tab2); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.menu_new: 
      Log.i("ActionBar", "New!"); 
      return true; 
     case R.id.menu_save: 
      Log.i("ActionBar", "Save!");; 
      return true; 
     case R.id.menu_settings: 
      Log.i("ActionBar", "Settings!");; 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
}} 

TabFragment1的代碼是這樣的:

public class Tab1Fragment extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment1, container, false); 
}} 

請幫幫我!

+0

對不起我新推出的個人電腦幫助 – 2013-03-22 00:51:00

回答

0
@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
super.onViewCreated(view, savedInstanceState); 

/**Fragments can initialize views from the xml layout file here or you could 
    * do it in the onCreateView() method with 
    * 
    * View fragmentView = inflater.inflate(R.layout.fragment1, container, false); 
    * mVebView = (WebView) fragmentView.findViewById(R.id.webview); 
    * return fragmentView; 
    * 
    * Activities initialize views in the OnCreate() method 
    */ 

mWebView = (WebView) view.findViewById(R.id.webview); 
mWebView.loadUrl("http://stackoverflow.com/questions/15560698/how-place-webview-in-tab-with-action-bar-android"); 

} 
+0

Thx dude !!問題已解決 – 2013-03-23 18:47:27