2016-04-29 70 views
0

我用Navigation Drawer創建了我的應用程序。 在我的活動我把這個代碼:如何把這個元素放在工具欄中?

public class MenuActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener 
{ 

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_menu); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) 
    { 
     public void onDrawerOpened(View view) 
     { 
      super.onDrawerOpened(view); 
     } 
     public void onDrawerClosed(View v) 
     { 
      super.onDrawerClosed(v); 
     } 
    }; 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    onNavigationItemSelected(navigationView.getMenu().getItem(0)); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.menu, menu); 
    return true; 
} 

public boolean onOptionsItemSelected(MenuItem item) 
{ 
    int id = item.getItemId(); 

    if (id == R.id.action_settings) 
    { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 


public boolean onNavigationItemSelected(MenuItem item) { 

    Fragment fragment = null; 
    String title = null; 
    int id = item.getItemId(); 
    Class fragmentClass=null; 

    if (id == R.id.Home) 
    { 
     fragmentClass = HomeFragment.class; 
     title="Home"; 

    } 
    else if (id == R.id.Profile) 
    { 
     fragmentClass= ProfileFragment.class; 
     title="Profile"; 
    } 
    try { 
     fragment = (Fragment) fragmentClass.newInstance(); 
    } catch (InstantiationException e) { 

     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } 


    FragmentManager fragmentManager = getSupportFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.content_frame,  fragment).commit(); 


    if (getSupportActionBar() != null) 
    { 
     getSupportActionBar().setTitle(title); 
    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 


    return true; 
} 

現在我想提出一個標籤在屏幕底部的HomeFragment。所以我把這個代碼:

public class HomeFragment extends Fragment { 


public static TabLayout tabLayout; 
public static ViewPager viewPager; 
public static int int_items = 3 ; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View inflatedView = inflater.inflate(R.layout.fragment_home, null); 


    tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabs); 
    viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager); 


    viewPager.setAdapter(new MyAdapter(getChildFragmentManager())); 


    tabLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      tabLayout.setupWithViewPager(viewPager); 
     } 
    }); 

    return inflatedView; 

} 

class MyAdapter extends FragmentPagerAdapter { 

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

    /** 
    * Return fragment with respect to Position . 
    */ 

    @Override 
    public Fragment getItem(int position) 
    { 
     switch (position){ 
      case 0 : return new SearchFragment(); 
      case 1 : return new CardFragment(); 
      case 2 : return new MapFragment(); 
     } 
     return null; 
    } 

    public int getCount() { 

     return int_items; 

    } 

但最後我得到這樣的結果:

enter image description here

我想這樣做:

  1. 把這個tablayout工具欄中的
  2. 把這個放在 屏幕的底部

我該怎麼辦?

回答

1

如果您希望TabLayout位於屏幕的底部,則不必將其實際放在工具欄中。如果它是特定於該片段,它應該留在那裏。在你的佈局嘗試這樣做:

<android.support.design.widget.TabLayout 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_gravity="bottom"> 

    </android.support.design.widget.TabLayout> 

根據應工作在父容器上,那麼你只要把它掛到您創建來處理每個標籤的ViewPager。如果是RelativeLayout則使用alignParentBottom而不是layout_gravity

此外,如果您希望在其他應用程序上滾動時它會消失,您仍然可以執行app:layout_anchor="@id/items_list"app:layout_anchorGravity="bottom"然後附加行爲以使其在滾動上滑出。它實際上並不需要它在AppBarLayout

也從您的基本視圖中刪除填充/邊距。

相關問題