[英]Navigation Drawer with tab in main page android xamarin
I make an app with Navigation Drawer. 我用导航抽屉制作了一个应用程序。 I want to add a tab in main page but when I run the app, i have an error in 我想在主页中添加一个标签,但是当我运行该应用程序时,出现错误
System.NullReferenceException: Object reference not set to an instance of an object. System.NullReferenceException:对象引用未设置为对象的实例。
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
how add tab? 如何添加标签? my code is: 我的代码是:
namespace NavigationDrawerLayout
{
[Activity(Label = "NavigationDrawerLayout", Theme = "@style/Theme.DesignDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : AppCompatActivity
{
DrawerLayout drawerLayout;
NavigationView navigationView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
// Add the tabs to Action Bar
AddTab("Tab One");
AddTab("Tab Two");
AddTab("Tab Three");
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
// Create ActionBarDrawerToggle button and add it to the toolbar
var toolbar = FindViewById<V7Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.drawer_open, Resource.String.drawer_close);
drawerLayout.SetDrawerListener(drawerToggle);
drawerToggle.SyncState();
navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
setupDrawerContent(navigationView);
}
private void AddTab(string tabText)
{
Android.App.ActionBar.Tab tab = ActionBar.NewTab();
tab.SetText(tabText);
tab.TabSelected += OnTabSelected;
ActionBar.AddTab(tab);
}
private void OnTabSelected(object sender, Android.App.ActionBar.TabEventArgs args)
{
var CurrentTab = (Android.App.ActionBar.Tab)sender;
if (CurrentTab.Position == 0)
{
}
else
{
}
}
void setupDrawerContent(NavigationView navigationView)
{
navigationView.NavigationItemSelected += (sender, e) => {
e.MenuItem.SetChecked(true);
drawerLayout.CloseDrawers();
};
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
navigationView.InflateMenu(Resource.Menu.nav_menu);
return true;
}
}
}
main.axml main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="fill_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/toolbar" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="300dp"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
styles.xml styles.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo">
</style>
<style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/ColorPrimary</item>
<item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
</style>
</resources>
System.NullReferenceException: Object reference not set to an instance of an object. System.NullReferenceException:对象引用未设置为对象的实例。
When you want to add a ActionBar
tab, your Activity
should have ActionBar
first, but in your Theme.DesignDemo
, you have delete it in your Activity
: 当您要添加ActionBar
选项卡时,您的Activity
应该首先具有ActionBar
,但是在Theme.DesignDemo
,您必须在Activity
Theme.DesignDemo
其删除:
<style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
It means you can't get the ActionBar
in your Activity
, and you have use Toolbar
instead. 这意味着您无法在Activity
获得ActionBar
,而改用Toolbar
。 That's why it throws NullReferenceException
exception. 这就是为什么它引发NullReferenceException
异常的原因。
how add tab? 如何添加标签?
With the API 21 the method ActionBarNavigationMode.Tabs
is deprecated . 使用API 21时, 不推荐使用 ActionBarNavigationMode.Tabs
方法。 You could use TabLayout in the Design Support Library . 您可以在设计支持库中使用TabLayout 。
Its usage is very simple, you could add it in your toolbar.axml
like this : 它的用法非常简单,您可以像这样将其添加到您的toolbar.axml
:
Toolbar.axml
: Toolbar.axml
:
<?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"
...
>
<android.support.design.widget.AppBarLayout
...
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:background="@color/colorPrimary" />
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.design.widget.TabItem
android:text="Tab One"/>
<android.support.design.widget.TabItem
android:text="Tab Two"/>
<android.support.design.widget.TabItem
android:text="Tab Three"/>
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
Use it in your code : 在您的代码中使用它:
TabLayout tabLayout = FindViewById<TabLayout>(Resource.Id.tablayout);
tabLayout.SetupWithViewPager(viewPager);
//The one-stop shop for setting up this TabLayout with a ViewPager
Here is its effect . 这是它的效果 。 For more information, you could read this document . 有关更多信息,您可以阅读本文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.