如何将此元素放在工具栏中?

时间:2016-04-29 15:18:11

标签: android tabs fragment

我使用导航抽屉创建了我的应用程序。 在我的活动中,我把这段代码:

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. 把它放在底部 屏
  3. 我怎么能这样做?

1 个答案:

答案 0 :(得分: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

同时从基本视图中删除填充/边距。