android Tablayout里面的导航片段ActionBarDrawerToggle图标不存在

时间:2017-02-01 11:22:37

标签: android navigation-drawer android-tablayout

带片段的导航抽屉

1 - 回家 2门课程 3 - 画廊

我为家庭,课程,画廊创建了3个片段。 当应用程序打开主页片段即将显示

当我点击导航抽屉课程中的课程时,片段将在此片段中打开我创建的标签布局和标签布局正确显示我需要,但抽屉切换图标不存在但抽屉在我从左侧拉动时打开< / p>

MainActivity.java:

public class MainActivity extends AppCompatActivity {
    DatabaseHelper databaseHelper;
    protected DrawerLayout drawerLayout;
    ActionBarDrawerToggle actionBarDrawerToggle;
    Toolbar toolbar;
    FragmentTransaction fragmentTransaction;

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


        toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
        actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        fragmentTransaction=getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.main_container,new HomeFragment());
        fragmentTransaction.commit();
        getSupportActionBar().setTitle("Home fragment");

        NavigationView navigationView= (NavigationView) findViewById(R.id.navview);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
        {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId())
                {
                    case R.id.Home:
                        fragmentTransaction=getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container,new HomeFragment());
                        fragmentTransaction.commit();

                        getSupportActionBar().setTitle("Home fragment");
                        item.setChecked(true);

                        break;
                    case R.id.gallery:
                        fragmentTransaction=getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container,new GalleryFragment());
                        fragmentTransaction.commit();

                        getSupportActionBar().setTitle("gallery fragment");
                        item.setChecked(true);

                        break;
                    case R.id.courses:
                        fragmentTransaction=getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container,new CoursesFragment());
                        fragmentTransaction.commit();


                        getSupportActionBar().setTitle("courses fragment");

                        item.setChecked(true);

                        break;
                }
                drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
                drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });

    }

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    tools:context="com.navdrawer.navdrawer.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"/>
    </LinearLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_container">

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/navview"
        android:layout_gravity="start"
        app:menu="@menu/drawer_menu"
        app:headerLayout="@layout/navigation_drawer_header"
        android:scrollbars="vertical"
        >
        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_marginBottom="-20dp"
            android:layout_marginLeft="-20dp"
            android:layout_gravity="bottom"
            android:src="@drawable/iso_main1"
            />

    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

CoursesFragment.java:

public class CoursesFragment extends Fragment {



    public CoursesFragment() {

        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_courses, container, false);

        Toolbar toolbar=(Toolbar)view.findViewById(R.id.toolbar);

        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
          ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Download");

        TabLayout tabLayout=(TabLayout)view.findViewById(R.id.courses_tabl);
        ViewPager viewPager=(ViewPager)view.findViewById(R.id.courses_viewpager);

        ViewpagerAdapter viewpagerAdapter=new ViewpagerAdapter(getChildFragmentManager());
        viewpagerAdapter.addFragments(new DownloadFragment(),"Download");
        viewpagerAdapter.addFragments(new AlreadyDownlodedFragment(),"Downloaded");
        viewPager.setAdapter(viewpagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        return view;
    }

fragment_courses.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.navdrawer.navdrawer.CoursesFragment">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/courses_appbar_layout"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
            <include
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                layout="@layout/toolbar_layout"
                />
        </LinearLayout>

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/courses_tabl"
            app:tabMode="fixed"
            app:tabGravity="fill"
            app:tabIndicatorColor="@android:color/white"

            >

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


    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/courses_viewpager"
        >

    </android.support.v4.view.ViewPager>


</FrameLayout>

这是我的fragment_gallery.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.navdrawer.navdrawer.GalleryFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Gallery"
        android:gravity="center"
        android:textSize="30sp"
        />

</FrameLayout>

GalleryFragment:

public class GalleryFragment extends Fragment {


    public GalleryFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_gallery, container, false);
    }

}

这是截图:

homefragment

courses fragment

gallery fragment

2 个答案:

答案 0 :(得分:1)

我能看到Gallery片段的xml吗? 顺便说一句,为什么你在课程片段中再次使用工具栏? 这一定是问题。从那里删除工具栏,因为它必须覆盖父活动的工具栏。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.navdrawer.navdrawer.CoursesFragment">



    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/courses_tabl"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabIndicatorColor="@android:color/white"/>


<android.support.v4.view.ViewPager
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/courses_viewpager"/>

这就是你需要的

还要在main_activity.xml中进行此更改

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <include
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            layout="@layout/toolbar_layout"/>
    </LinearLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_container">

    </FrameLayout>

答案 1 :(得分:0)

您可以按如下方式设置主页按钮

getSupportActionBar().setHomeButtonEnabled(true);

setSupportActionBar(toolbar);之后添加此行 如果要设置自定义图标,则可以设置如下

toolbar.setNavigationIcon(R.drawable.myhome);//pass id of your navigation icon