【发布时间】:2018-09-19 18:09:03
【问题描述】:
我尝试将 Toolbar、TabBarLayout 和 BottomNavigationView 添加到我的活动中。 不添加工具栏,一切正常,但添加工具栏后,应用程序崩溃并报错:
Android.Views.InflateException:二进制 XML 文件第 1 行:二进制 XML 文件第 1 行:膨胀类错误 android.support.design.widget.BottomNavigationView
我正在使用线性布局。我尝试通过更改 layout_weight 和主题以及我可以在 stackoverflow 上找到或通过搜索 Google 找到的所有其他方法,但找不到解决方案。我正在分享我的布局、样式和活动代码。
activity_main.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"
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>
MainActivity.cs
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;
}
} }
style.xml
<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>
ToolbarLayout.axml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:background="@color/white"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
谁能帮我解决这个问题?
【问题讨论】:
-
关于通货膨胀应该有更详细的错误。
-
请发布您的依赖项
标签: c# android visual-studio xamarin.android