我试图将Toolbar,TabBarLayout和BottomNavigationView添加到我的活动中。 在不添加工具栏的情况下,一切正常,但是在添加工具栏后,应用程序崩溃并显示错误:
Android.Views.InflateException:二进制XML文件第1行:二进制XML 文件行#1:错误夸大类 android.support.design.widget.BottomNavigationView
我正在使用LinearLayout。我尝试通过更改layout_weight和theme以及我可以在stackoverflow上找到的所有其他方法或通过搜索Google进行尝试,但是找不到解决方案。我正在分享布局,样式和活动代码。
activity_main.axml
dataGridView1.DataSource = results;
MainActivity.cs
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100">
<include
android:id="@+id/toolbar"
layout="@layout/ToolbarLayout" />
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/tablayout"
android:background="@color/white"
android:minHeight="?attr/actionBarSize"
app:tabTextColor="@color/black"
app:tabSelectedTextColor="@color/yellow"
app:tabIndicatorColor="@color/yellow"
android:layout_weight="10"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/pager"
android:layout_weight="82">
</android.support.v4.view.ViewPager>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation"
android:layout_weight="8"/>
</LinearLayout>
style.xml
using Android.App; using Android.OS; using Android.Runtime; using Android.Support.Design.Widget; using Android.Support.V4.View; using Android.Support.V7.App; using Android.Support.V7.Widget; using Android.Views;
namespace utfive {
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme")]
public class MainActivity : AppCompatActivity, BottomNavigationView.IOnNavigationItemSelectedListener
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
SetSupportActionBar(FindViewById<Toolbar>(Resource.Id.toolbar));
BottomNavigationView navigation = FindViewById<BottomNavigationView>(Resource.Id.navigation);
navigation.SetOnNavigationItemSelectedListener(this);
LoadPager();
}
private void LoadPager()
{
ViewPager viewPager = (ViewPager)FindViewById(Resource.Id.pager);
PageAdapter myPagerAdapter = new PageAdapter(SupportFragmentManager);
viewPager.Adapter = myPagerAdapter;
TabLayout tabLayout = (TabLayout)FindViewById(Resource.Id.tablayout);
tabLayout.SetupWithViewPager(viewPager);
}
public bool OnNavigationItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.navigation_home:
//textMessage.SetText(Resource.String.title_home);
return true;
case Resource.Id.navigation_dashboard:
//textMessage.SetText(Resource.String.title_dashboard);
return true;
case Resource.Id.navigation_notifications:
//textMessage.SetText(Resource.String.title_notifications);
return true;
}
return false;
}
} }
ToolbarLayout.axml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="colorPrimary">@color/yellow</item>
<item name="colorPrimaryDark">@color/black</item>
<item name="colorAccent">@color/white</item>
</style>
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/yellow</item>
<item name="colorPrimaryDark">@color/black</item>
<item name="colorAccent">@color/white</item>
</style>
</resources>
有人可以帮我吗?
答案 0 :(得分:0)
从包括标记中删除ID属性:android:id="@+id/toolbar"
。它应该看起来像这样
layout="@layout/toolbar_layout" />
您的ToolbarLayout文件名不正确,应该是这样的:toolbar_layout不带大写字母。
答案 1 :(得分:0)
发布问题后,我仍然尝试解决问题。 我清除了解决方案,然后重新启动Visual Studio。 然后我清除了项目,然后再次重新启动Visual Studio,然后一切正常。