用于底部导航栏的库是 - https://github.com/pocheshire/BottomNavigationBar
我的应用程序中有导航抽屉,因此我遇到底部导航栏位于导航抽屉上方的问题。所以我遵循了文档中提供的建议:
您需要做的就是将BottomBar附加到您的Activity,而不是将其附加到包含您内容的视图。例如,如果您的片段位于具有id fragmentContainer
的ViewGroup中
所以我做到了这一点:
protected override void OnCreate(Bundle bundle)
{
// Some stuff
bottomBar = BottomBar.Attach(this.FindViewById<FrameLayout>(Resource.Id.fragmentContainer), bundle);
bottomBar.SetItems(Resource.Menu.bottombar_menu);
bottomBar.SetOnMenuTabClickListener(this);
bottomBar.SetGravity(GravityFlags.Bottom);
var layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
layoutParams.Gravity = GravityFlags.Bottom;
bottomBar.LayoutParameters = layoutParams;
bottomBar.NoTopOffset();
// Make a Badge for the first tab, with red background color and a value of "13".
BottomBarBadge unreadMessages = bottomBar.MakeBadgeForTabAt(0, "#FF0000", 13);
// Control the badge's visibility
unreadMessages.Show();
unreadMessages.Hide();
// Change the displayed count for this badge.
unreadMessages.Count = 4;
// Change the show / hide animation duration.
unreadMessages.AnimationDuration = 200;
// If you want the badge be shown always after unselecting the tab that contains it.
unreadMessages.AutoShowAfterUnSelection = true;
var fragment = this.FragmentManager.BeginTransaction();
fragment.AddToBackStack(null);
fragment.Add(Resource.Id.fragmentContainer, new CustomFragment());
fragment.Commit();
}
但不幸的是结果Bottom Navigation Bar is visible, but the content of Fragment is not。
我查了UI Hierarchy,我的片段内容甚至没有添加。
我还尝试在我附加底部导航栏的视图下添加简单的LinearLayout,它也被底部导航栏覆盖。
以下是我的主要活动视图的AXML:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:fitsSystemWindows="true">
<android.support.v4.widget.DrawerLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/drawer_layout">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/toolbar"
android:id="@+id/app_bar"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/app_bar">
<FrameLayout
android:id="@+id/fragmentContainer"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</ScrollView>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/nav_menu"
app:headerLayout="@layout/menu_header" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
编辑:
以下是使用的其他资源文件:
bottom_bar.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/bottomBarItem_Taken"
android:icon="@drawable/ic_file_download_white_48dp"
android:title="" />
<item
android:id="@+id/bottomBarItem_Given"
android:icon="@drawable/ic_file_upload_white_48dp"
android:title="" />
<item
android:id="@+id/bottomBarItem_Discover"
android:icon="@drawable/ic_cached_white_48dp"
android:title="" />
</menu>
nav_menu.xml
<?xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home" />
<item
android:id="@+id/nav_favorites"
android:icon="@drawable/ic_favorite_black_24dp"
android:title="Favorites" />
<item
android:id="@+id/nav_stuff"
android:icon="@drawable/ic_business_center_black_24dp"
android:title="My Stuff" />
<item
android:id="@+id/nav_contacts"
android:icon="@drawable/ic_contacts_black_24dp"
android:title="Contacts" />
<item
android:id="@+id/nav_messages"
android:icon="@drawable/ic_message_black_24dp"
android:title="Messages" />
<item
android:id="@+id/nav_calendar"
android:icon="@drawable/ic_date_range_black_24dp"
android:title="Calendar" />
<item
android:id="@+id/nav_payments"
android:icon="@drawable/ic_payment_black_24dp"
android:title="Payments" />
<item
android:id="@+id/nav_history"
android:icon="@drawable/ic_history_black_24dp"
android:title="History" />
</group>
<item>
<menu>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings_black_24dp"
android:title="Settings" />
<item
android:id="@+id/nav_systemNotifications"
android:icon="@drawable/ic_android_black_24dp"
android:title="System Notifications" />
<item
android:id="@+id/nav_support"
android:icon="@drawable/ic_info_outline_black_24dp"
android:title="Support" />
<item
android:id="@+id/nav_aboutUs"
android:icon="@drawable/ic_help_outline_black_24dp"
android:title="About Us" />
</menu>
</item>
</menu>
menu_header.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="178dp"
android:orientation="vertical"
android:weightSum="1"
android:background="#00BCD4">
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/imageView1"
android:gravity="center_horizontal">
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@drawable/profile"
android:layout_gravity="center_horizontal"
/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="54dp"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/linearMenuHeader"
android:layout_below="@id/imageView1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textColor="#ffffff"
android:text="John Johnson"
android:textSize="20dp"
android:typeface="sans" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:layout_marginLeft="16dp"
android:layout_marginTop="2dp"
android:text="ONLINE"
android:textSize="14dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>