几个小时后,我终于找到了使非常精致的 BottomNavigationView 工作的代码。但是,我还需要将 FloatingActionButton 与BottomNavigationView结合使用...不一定必须使用相同的布局,但是即使我将其包含在其他布局中也不需要似乎有效。
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="0.1 (Beta)" package="com.PandtacularApps.FiveEStoreGen" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<application android:label="@string/app_name" android:icon="@drawable/icon"></application>
</manifest>
我已检查并正在使用最新的Xamarin NuGet软件包(v27.0.2.1):
Xamarin.Android.Arch.Core.Common
Xamarin.Android.Arch.Lifecycle.Common
Xamarin.Android.Arch.Lifecycle.Runtime
Xamarin.Android.Support.Animated.Vector.Drawable
Xamarin.Android.Support.Annotations
Xamarin.Android.Support.Compat
Xamarin.Android.Support.Core.UI
Xamarin.Android.Support.Core.Utils
Xamarin.Android.Support.Design
Xamarin.Android.Support.Fragment
Xamarin.Android.Support.Media.Compat
Xamarin.Android.Support.Transition
Xamarin.Android.Support.v7.AppCompat
Xamarin.Android.Support.v7.RecyclerView
Xamarin.Android.Support.Vector.Drawable
我还验证了 Xamarin.Android.Support.Design 和 Xamarin.Android.Support.v7.AppCompat 的版本(v4.0.30319)与我在研究中发现这有时可能是个问题。
如果我自己拥有BottomNavigationView,将FloatingActionButtom注释掉,则显示效果很好。如果我自己拥有FloatingActionButton,则将BottomNavigationView注释掉,则显示效果很好。第二个我都拥有,当我命中时抛出一个异常:
SetContentView(Resource.Layout.activity_main);
例外是:
Binary XML file line #5: Binary XML file line #5: Error inflating class android.support.design.widget.BottomNavigationView
完整堆栈跟踪:
--- End of managed Android.Views.InflateException stack trace ---
android.view.InflateException: Binary XML file line #5: Binary XML file line #5: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at md538149be85481df038e356841962feb89.MainScreen.n_onCreate(Native Method)
at md538149be85481df038e356841962feb89.MainScreen.onCreate(MainScreen.java:31)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTextColor(android.content.res.ColorStateList)' on a null object reference
at android.support.design.internal.BottomNavigationItemView.setTextColor(BottomNavigationItemView.java:249)
at android.support.design.internal.BottomNavigationMenuView.buildMenuView(BottomNavigationMenuView.java:278)
at android.support.design.internal.BottomNavigationPresenter.updateMenuView(BottomNavigationPresenter.java:62)
at android.support.design.widget.BottomNavigationView.inflateMenu(BottomNavigationView.java:236)
at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:165)
at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:114)
... 27 more
activity_main布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar -->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
</android.support.design.widget.AppBarLayout>
<!-- Content -->
<!--<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->
<!--<include
layout="@layout/content_main" />-->
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:srcCompat="@android:drawable/ic_dialog_email" />
<!-- BottomNavigationView -->
<android.support.design.widget.BottomNavigationView
android:id="@+id/bnv"
android:layout_height="56dp"
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
app:itemIconTint="@drawable/nav_item_colors"
app:itemTextColor="@drawable/nav_item_colors"
android:background="@android:color/white"
app:elevation="6dp"
app:menu="@menu/menu_bottom" />
</android.support.design.widget.CoordinatorLayout>
menu_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_bottom_stores"
android:enabled="true"
android:icon="@drawable/places"
android:title="@string/stores"
app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_bottom_items"
android:enabled="true"
android:icon="@drawable/list"
android:title="@string/items"
app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_bottom_settings"
android:enabled="true"
android:icon="@drawable/cog"
android:title="@string/settings"
app:showAsAction="ifRoom" />
</menu>
nav_item_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@android:color/black" />
<item android:color="#80000000" />
</selector>
styles.xml:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="Divider">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">?android:attr/listDivider</item>
</style>
</resources>
我的主要活动:
using System;
using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Text;
using Android.Text.Method;
using Android.Views;
namespace MyNamespace
{
/// <summary>The primary application screen</summary>
[Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/AppTheme.NoActionBar")]
public class MainScreen : AppCompatActivity
{
// -------------------------------------------------------------------
#region Protected Methods
/// <inheritdoc />
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
}
#endregion
// -------------------------------------------------------------------
#region Public Methods
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu_main, menu);
return true;
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
int id = item.ItemId;
if (id == Resource.Id.action_settings)
{
return true;
}
return base.OnOptionsItemSelected(item);
}
#endregion
}
}
我一直在疯狂地试图找出这个愚蠢的控件,如果您看错了它似乎会抛出异常……任何帮助将不胜感激