2014-02-05 43 views
0

我試圖使用標籤欄/操作欄彈出對話框,但我在對話框後面顯示對話框和標籤欄。如何獲得對話框中的標籤欄?我非常感謝任何幫助。感謝提前。在對話框中獲取操作欄/導航欄

public class MainActivity extends Activity implements TabListener { 

     final String[] page_titles = new String[]{"Home", "contact", "Apps"}; 
     //this will go the description TextView 
      final String[] desc = new String[]{ 
       "This is the homepage ", 
       "contact", 
       "apps" 
     }; 
    ViewPager pager; 

    ActionBar bar; 

    Dialog dialog; 

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


     dialog = new Dialog(MainActivity.this); 

     dialog.setContentView(R.layout.activity_main); 



      pager = (ViewPager)dialog. findViewById(R.id.pager); 

      bar = getActionBar(); 

      myAdaptertest1 mAdapter = new myAdaptertest1(this); 

      bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

      bar.setHomeButtonEnabled(false); 

      pager.setAdapter(mAdapter); 
      pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 

       @Override 
       public void onPageSelected(int position) { 

        bar.setSelectedNavigationItem(position); 
       } 
      }); 



      Tab hometab = bar.newTab(); 
      hometab.setText("Home"); 
      hometab.setTabListener(this); 
      bar.addTab(hometab); 

      Tab cameratab = bar.newTab(); 
      cameratab.setText("contact"); 
      cameratab.setTabListener(this); 
      bar.addTab(cameratab); 

      Tab exploretab = bar.newTab(); 
      exploretab.setText("apps"); 
      exploretab.setTabListener(this); 
      bar.addTab(exploretab); 


      bar.show(); 
      bar.setDisplayShowTitleEnabled(false); 
      bar.setDisplayShowHomeEnabled(false); 


      dialog.show(); 
    } 


    private class myAdaptertest1 extends PagerAdapter{ 

     Context context; 

     public myAdaptertest1(Context c){ 
      this.context = c; 
     } 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
       return page_titles.length; 
     } 

     @Override 
     public boolean isViewFromObject(View v, Object o) { 
      // TODO Auto-generated method stub 
       return v.equals(o); 
     } 





     public Object instantiateItem(View pager, int position) { 
      final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View vi = inflater.inflate(R.layout.page, null, false); 

      TextView title = (TextView)vi.findViewById(R.id.tvTitle); 
      TextView description = (TextView) vi.findViewById(R.id.tvdesc); 

      title.setText(page_titles[position]); 
      description.setText(desc[position]); 

      //This is very important 
      ((ViewPager) pager).addView(vi, 0); 

      return vi; 
     } 

     @Override 
     public void destroyItem(View pager, int position, Object view) { 
      ((ViewPager) pager).removeView((View) view); 
     } 


    } 


    @Override 
    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 

    } 


    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 
     pager.setCurrentItem(tab.getPosition()); 
    } 


    @Override 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 

    } 


    public static void showAsPopup(Activity activity) { 
     //To show activity as dialog and dim the background, you need to declare android:theme="@style/PopupTheme" on for the chosen activity on the manifest 
     activity.requestWindowFeature(Window.FEATURE_ACTION_BAR); 
     activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, 
       WindowManager.LayoutParams.FLAG_DIM_BEHIND); 
     LayoutParams params = activity.getWindow().getAttributes(); 
     params.height = LayoutParams.FILL_PARENT; 
     params.width = 850; //fixed width 
     params.alpha = 1.0f; 
     params.dimAmount = 0.5f; 
     activity.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params); 
    } 

} 



<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<android.support.v4.view.ViewPager 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/pager"/> 
</LinearLayout> 
+0

只是爲了澄清:你希望你的對話框有一個操作欄? ......爲什麼有什麼特別的理由? – kentarosu

+0

是的。基本上我使用滑動抽屜,當我點擊其中一個選項時,我需要打開如上所述的導航選項卡的對話框。因此,當用戶按下後,對話框消失。如果上述可能,讓我知道如何實現這個? – jason

回答

0

如果你想要的是標籤(這意味着你不需要你的對話框中的操作欄),試圖在定製對話框佈局添加TabHostTabWidget秒。你應該不要把一個操作欄放在對話框中。

+0

我想要標籤和滑動too.is可能嗎? – jason

+0

在ViewPager中滑動?再次......爲什麼在對話中需要所有這些?當你點擊滑動抽屜裏的東西時,爲什麼不改變一個片段呢? – kentarosu

+0

我沒有在任何地方使用片段,我在整個應用程序中使用活動。所以有可能這樣做嗎?此外,我從來沒有使用過片段,所以我不知道與活動相比的行爲。 – jason