2015-12-19 74 views
3

我正在開發一個應用程序,其中在我的MainActivity中我使用導航抽屜切換幀佈局上的片段。TabLayout與片段上的ViewPager - Lolipop設備

其中一個片段是「幫助」片段,其中我使用的標籤佈局和viewpager三頁中刷卡,「關於我們」,「幫助」 &「聯繫我們」

一切正常罰款在下面的棒棒糖devices.The操作欄似乎堅持選項卡布局(使用工具欄作爲一個操作欄)。

但是在棒棒糖設備上,它顯示爲像操作欄和tablayout分開。

我該如何在棒棒糖設備上實現同樣的效果呢?

MainActivity:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

NavigationView navigationView; 
ActionBarDrawerToggle toggle; 
DrawerLayout drawer; 
FragmentManager fm = getSupportFragmentManager(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle("Title"); 

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.nav_help) { 
     getSupportActionBar().setTitle("Help"); 
     fm.beginTransaction().replace(R.id.l_frame_layout, new Help(),"help").commit(); 
    } 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true;} 

Help.java

public class Help extends Fragment { 
ViewPager pager; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.layout_help, container, false); 
    return(rootView); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    final TabLayout tabLayout = (TabLayout)getActivity(). findViewById(R.id.tllayout_helpTABLAYOUTtl); 
    tabLayout.addTab(tabLayout.newTab().setText("ABOUT")); 
    tabLayout.addTab(tabLayout.newTab().setText("HELP")); 
    tabLayout.addTab(tabLayout.newTab().setText("CONTACT")); 

    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 
    actionBar = getActivity().getActionBar(); 

    pager = (ViewPager)getActivity().findViewById(R.id.vplayout_helpVIEVPAGERvp); 
    pager.setAdapter(new MyPagerAdapter(getFragmentManager())); 

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
     @Override 
     public void onTabSelected(TabLayout.Tab tab) { 
      pager.setCurrentItem(tab.getPosition()); 
     } 
     @Override 
     public void onTabUnselected(TabLayout.Tab tab) { 

     } 
     @Override 
     public void onTabReselected(TabLayout.Tab tab) { 

     } 
    }); 

    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 
     @Override 
     public void onPageSelected(int position) { 


     } 
     @Override 
     public void onPageScrollStateChanged(int state) { 

     } 
    }); 
} 

class MyPagerAdapter extends FragmentStatePagerAdapter { 
    public MyPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int pos) { 
     switch(pos) { 

      case 0: AboutUs tab1 = new AboutUs(); 
       return tab1; 

      case 1: HelpRecharge tab2 = new HelpRecharge(); 
       return tab2; 

      case 2: ContactUs tab3 = new ContactUs(); 
       return tab3; 

      default: return null; 
     } 
    } 

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

enter image description here

+0

請你加入活動代碼在這裏? – Mohsen

+0

發佈您的xml代碼 –

+0

這可能有助於http://stackoverflow.com/a/26725601/1709595 –

回答

1

嘗試設置工具欄的高度動態地加入以下代碼:

 appBarLayout.setElevation(0); 
2

好像你有海拔在你的風格XML操作欄設置。

對於Android 5.0以上版本,如果你想爲在下面設置動作條的風格:

<item name="android:elevation">0dp</item> 

和支持庫兼容使用:

<item name="elevation">0dp</item> 

風格的程序兼容性光主題示例:

<style name="Theme.MyApp.ActionBar" parent="style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> 
    <!-- remove shadow below action bar --> 
    <!-- <item name="android:elevation">0dp</item> --> 
    <!-- Support library compatibility --> 
    <item name="elevation">0dp</item> 
</style> 

然後將此自定義ActionBar樣式應用於您的應用主題:

<style name="Theme.MyApp" parent="Theme.AppCompat.Light"> 
    <item name="actionBarStyle">@style/Theme.MyApp.ActionBar</item> 
</style> 

而且預5.0的Android,也添加到您的應用主題:

<!-- Remove shadow below action bar Android < 5.0 --> 
<item name="android:windowContentOverlay">@null</item>