2011-07-01 3 views
3

新しく開発されたAndroid開発版です。 私は3つの異なるタブを持つアプリケーションを開発しています。私はすべてのタブと共通のメニューオプション(AboutやRefreshなど)を各個別の必要に応じて個別のメニューに必要とします。さまざまなタブに共通するメニュー

myappのメイン():他の[インベントリ]タブで

public class MyApp extends TabActivity { 
    /** Called when the activity is first created. */ 

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     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 

     // Create an Intent to launch an Activity for the tab (to be reused) 
     intent = new Intent().setClass(this, Bundles.class); 
     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("bundles").setIndicator("Bundles", 
          res.getDrawable(R.drawable.ic_tab_bundle_grey)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

     // Do the same for the other tabs 
     intent = new Intent().setClass(this, Policies.class); 
     spec = tabHost.newTabSpec("policies").setIndicator("Policies", 
          res.getDrawable(R.drawable.ic_tab_policy_grey)) 
         .setContent(intent); 
     tabHost.addTab(spec); 

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

     tabHost.setCurrentTab(2); 
    } 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.layout.menu, menu); 
    return true; 
} 

public void onClick(View v) { 
    switch(v.getId()) { 
    case R.id.about: 
     Intent i = new Intent(this, About.class); 
     startActivity(i); 
     break;    
    case R.id.refresh: 
     Intent j = new Intent(this, RefreshAgent.class); 
     startActivity(j); 
     break; 
    // more buttons to go here later 
    } 
} 

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.layout.settings_menu, menu); 
     return true; 
    } 

    public void onClick(View v) { 
     switch(v.getId()) { 
     case R.id.register: 
      Intent i = new Intent(this, About.class); 
      startActivity(i); 
      break;    
     case R.id.reregister: 
      Intent j = new Intent(this, Reregister.class); 
      startActivity(j); 
      break; 
     case R.id.accountsettings: 
      Toast.makeText(this, "Account Settings", Toast.LENGTH_LONG).show(); 
      break; 

     } 
    } 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.register: 
      Toast.makeText(this, "Registration Not implemented!", Toast.LENGTH_LONG).show(); 
      return true; 
     case R.id.reregister: 
      Toast.makeText(this, "Re Registration Not implemented!", Toast.LENGTH_LONG).show(); 
      //TODO : Use refresh agent class 
      //startActivity(new Intent(this, RefreshAgent.class)); 
      return true; 
     case R.id.accountsettings: 
      //Intent j = new Intent(this, RefreshAgent.class); 
      //startActivity(j); 
      Toast.makeText(this, "Account Settings", Toast.LENGTH_LONG).show(); 
      break; 
     } 
     return false; 

実装ディスプレイ上約及びその他の2つのタブを更新しますが、[インベントリ]タブで、それは2つだけ定義されているが表示されます[インベントリ]タブにあります。

ご協力いただきありがとうございます。

答えて

1

あなたの2つのコードサンプルが同じクラス(私はtabactivityを使用したことはありません)かどうかはわかりませんでしたが、どちらの場合もanwserになります。

  • 在庫タブがMainAppにより、別のクラスにある場合:最初のコードサンプルで

、あなたが

inflater.inflate(R.layout.menu, menu); 

を行うことを約リフレッシュメニューを表示し、このラインです。 あなたのインベントリタブではaboutとrefreshが含まれていないR.layout.settings_menuを膨らませます。そのため、すべてのタブに共通メニューが表示されません。

すべてのタブでR.layout.menuをinflateする必要があります(common_menuの名前を変更することをお勧めします)。

例では、在庫]タブで、あなただけのことを実行する必要があります。

MenuInflater inflater = getMenuInflater(); 
inflater.inflate(R.layout.menu, menu); 
inflater.inflate(R.layout.settings_menu, menu); 
return true; 

メニューの順に膨張しているので、あなたは一番下に、共通のメニューが表示され、上に特定のメニュー。

あなたが動機付けられている場合は、共通メニューのonCreateOptionMenuを膨らませる「基本アクティビティ」を実行し、特定のアクティビティ(基本アクティビティを拡張する人)でsuper.onCreateOptionMenu後に必要なものを膨らませる。

  • 同じクラスにあります場合:

あなたは示した現在のタブの機能に別のメニューを膨らませる必要があります。難しい問題が1つあります。onCreateOptionMenuはアクティビティで1回しか呼び出されないため、タブを変更してメニューを押すと、先行メニューが表示されます。

Fortunatelly、あなたも使用できる別の方法があります。これはonPrepareOptionMenu()です。このメソッドは、アプリケーションにメニューを表示するたびに呼び出されます。だから、あなたはここであなたの特定のメニューを膨らませる必要があります。 あなたは、このリンクを読むことができます:http://developer.android.com/guide/topics/ui/menus.html

をしかし、基本的に、あなたはちょうどそれをする必要があります。

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
MenuInflater inflater = getMenuInflater(); 
    if(Tab == Inventory) { 
     inflater.inflate(R.layout.settings_menu, menu); 
    } 
return true; 
} 

、共通のメニューを毎回膨らませるだろう、あなたの最初のonCreateOptionMenuを、保ちます。

+0

素晴らしい! inflater.inflate(R.layout.menu、menu)を追加していただきありがとうございます。在庫タブの下でそれはうまくいった:) –

関連する問題