How to make navigation drawer and bottom navigation with the same app?

问题: I'm trying to use navigation drawer and bottom bar nav in my app.therefore i have created navigation activity first.then i tried to add bottom bar nav to that same activity...

问题:

I'm trying to use navigation drawer and bottom bar nav in my app.therefore i have created navigation activity first.then i tried to add bottom bar nav to that same activity. I want to develop like this app:

without BottomNavigationView in Activity.xml,app is working.but when i add BottomNavigationView inside Activity.xml app crashed.nothing showing in logcat. how can i use both bottombar nav and navigation drawer in same activity,please give me an simple example? thx


回答1:

Add a parent to <include ..../> then add BottomNavigationView

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
     <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:itemTextColor="@color/colorAccent"
        app:menu="@menu/bottom_navigation_menu"/>

</RelativeLayout>

回答2:

Use TabLayout instead of bottom navigation TabLayout is better and easier

create root.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/drawerlayout"
    android:layout_height="match_parent">
    <include layout="@layout/activity_main"/>
    <include layout="@layout/navi_drawer"/>    
</android.support.v4.widget.DrawerLayout>

create navi_drawer

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:background="?attr/navigationBackground"
        android:orientation="vertical">
    </LinearLayout>

and activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/backgroundActivity"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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


    <LinearLayout
        android:id="@+id/lnrTab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutDirection="rtl"
        android:orientation="vertical"
        app:layout_anchor="@+id/viewpager"
        app:layout_anchorGravity="bottom|center">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/dividers_color_dark"
            app:layout_anchor="@+id/viewpager"
            app:layout_anchorGravity="bottom|center" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            app:tabBackground="?attr/backgroundTab"
            app:tabContentStart="9dp"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabIndicatorHeight="1dp"
            app:tabMode="scrollable" />
    </LinearLayout>


</LinearLayout>

then MainActivity

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

            //config your TabLayout
    }
  • 发表于 2018-12-29 19:58
  • 阅读 ( 1581 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除