2013-10-24 54 views
0

我想知道導航標籤是否在操作欄內或其下方。在第一種(非有效)方法中,我假設行動欄位於橫向的行動欄內,並位於縱向行動欄的下方。這是測試活動代碼:在動作欄或其下面的導航標籤?

public class TabTestActivity extends FragmentActivity implements 
     ActionBar.TabListener { 

    SectionsPagerAdapter mSectionsPagerAdapter; 
    ViewPager mViewPager; 
    private AlertDialog.Builder dialogBuilder; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_tab_test); 
     dialogBuilder = new AlertDialog.Builder(this); 
     dialogBuilder.setTitle("Tabs inside or below action bar?"); 
     dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
      } 
     }); 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
     mViewPager 
       .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
        @Override 
        public void onPageSelected(int position) { 
         actionBar.setSelectedNavigationItem(position); 
        } 
       }); 

     for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
      actionBar.addTab(actionBar.newTab() 
        .setText(mSectionsPagerAdapter.getPageTitle(i)) 
        .setTabListener(this)); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.tab_test, menu); 
     return true; 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
     mViewPager.setCurrentItem(tab.getPosition()); 
     AlertDialog notifyTabsLocationDialog = dialogBuilder.create(); 
     // First approach: assume that tabs are inside action bar in landscape 
     // and below action bar in landscape, but they are always inside action 
     // bar for my tablet 
     if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
      notifyTabsLocationDialog.setMessage("Tabs below action bar"); 
     } else { 
      notifyTabsLocationDialog.setMessage("Tabs inside action bar");    
     } 
     notifyTabsLocationDialog.show(); 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, 
      FragmentTransaction fragmentTransaction) { 
    } 

    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      Fragment fragment = new DummySectionFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     @Override 
     public int getCount() { 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(l); 
      case 2: 
       return getString(R.string.title_section3).toUpperCase(l); 
      } 
      return null; 
     } 
    } 

    public static class DummySectionFragment extends Fragment { 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public DummySectionFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_tab_test_dummy, 
        container, false); 
      TextView dummyTextView = (TextView) rootView 
        .findViewById(R.id.section_label); 
      dummyTextView.setText(Integer.toString(getArguments().getInt(
        ARG_SECTION_NUMBER))); 
      return rootView; 
     } 

    } 

} 

回答

0

我使用反射來訪問一個布爾場mHasEmbeddedTabsActionBar的發現workaraound:

Class<?> actionBarClass = getActionBar().getClass(); 
Field mHasEmbeddedTabs = actionBarClass.getDeclaredField("mHasEmbeddedTabs"); 
mHasEmbeddedTabs.setAccessible(true); 
if (mHasEmbeddedTabs.getBoolean(getActionBar())) { 
    notifyTabsLocationDialog.setMessage("Tabs inside action bar");    
} else { 
    notifyTabsLocationDialog.setMessage("Tabs below action bar"); 
} 
+1

這種方法並不是所有未來的證明,對於軟糖設備,您必須使用getActionBar.getSuperClass.getClass,而對於Kit Kat,您必須使用getActionBar.getSuperClass()。getSuperClass()。getClass()以獲得ICS使用正確方法的actionbar類。我找到的唯一好的解決方案是忍受難看的風景標籤。 –

1

從動作條指南:Adding Navigation Tabs

由動作條提供的標籤是理想的,因爲它們能夠適應不同的屏幕尺寸。例如,當屏幕足夠寬時,選項卡會出現在動作欄旁邊的動作按鈕上(例如在平板電腦上時(如圖7所示)),而在窄屏幕上時,它們會出現在單獨的欄中(稱爲「堆疊的動作條」,如圖8所示)。在某些情況下,Android系統會將您的標籤項目顯示爲一個下拉列表,以確保最適合操作欄。

是的,你的假設是安靜的。但該參數不是縱向或橫向,但有足夠的空間或沒有足夠的空間來將標籤與操作欄合併。

+0

我敢肯定它不僅可用房間的一個問題,因爲我在縱向模式下平板電腦只有480像素的寬度,它仍然將標籤放置在操作欄內。 –