我在我的項目中使用custom TabbedPage
在Tab bar
中顯示badges(count)
。我正在使用下面的代碼來顯示custom tab bar
,但它總是返回null
值CustomTabbedPageRenderer.cs
類OnWindowVisibilityChanged
方法的activity.ActionBar
。我已經嘗試了許多解決辦法如改變Theme
爲Theme.AppCompat.Light.DarkActionBar
和加入Window.RequestFeature(WindowFeatures.ActionBar);
MainActivity.cs下面的線,但遺憾的是這些並沒有幫助我。Xamarin形式Android自定義TabbedPage ActionBar返回null
MainActivity.cs
namespace Bakery.Droid
{
[Activity(Label = "Bakery.Droid", Icon = "@mipmap/icon_launcher", Theme = "@style/MyTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
}
Home.cs
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Bakery
{
public class Home : CustomTabPage
{
public Home()
{
NavigationPage.SetHasNavigationBar(this, false);
var burger = new NavigationPage(new Burger());
burger.Icon = "burger.png";
burger.Title = "Burger";
var sandwich = new NavigationPage(new Sandwich());
sandwich.Icon = "sandwich.png";
sandwich.Title = "Sandwich";
var pizza = new NavigationPage(new Pizza());
pizza.Icon = "pizza.png";
pizza.Title = "Pizza";
var roll = new NavigationPage(new Roll());
roll.Icon = "roll.png";
roll.Title = "Roll";
//adding childrens into the tab
Children.Clear();
Children.Add(burger);
Children.Add(sandwich);
Children.Add(pizza);
Children.Add(roll);
}
protected override void OnAppearing()
{
base.OnAppearing();
}
}
}
CustomTabPage.cs
using System;
using Xamarin.Forms;
namespace Bakery
{
public class CustomTabPage : TabbedPage
{
}
}
個styles.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<style name="MyTheme" parent="MyTheme.Base">
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">true</item>
<item name="windowActionModeOverlay">true</item>
</style>
</resources>
CustomTabbedPageRenderer.cs
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace Bakery.Droid
{
public class CustomTabbedPageRenderer : TabbedRenderer
{
Activity activity;
List<string> filenames;
protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
{
base.OnElementChanged(e);
filenames = e.NewElement.Children.Select(t => t.Icon.File).ToList();
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
activity = this.Context as Activity;
}
protected override void OnWindowVisibilityChanged(Android.Views.ViewStates visibility)
{
try
{
base.OnWindowVisibilityChanged(visibility);
var actionBar = activity.ActionBar;
var colorDrawable = new ColorDrawable(Android.Graphics.Color.Yellow);
actionBar.SetStackedBackgroundDrawable(colorDrawable);
System.Diagnostics.Debug.WriteLine("Onwindow visible");
ActionBarTabsSetup(actionBar);
}
catch (Exception Exception)
{
System.Diagnostics.Debug.WriteLine("Exception: " + Exception.ToString());
}
}
void ActionBarTabsSetup(ActionBar actionBar)
{
for (var i = 0; i < actionBar.NavigationItemCount; ++i)
{
var tab = actionBar.GetTabAt(i);
var id = GetImageFromFilename(i);
if (id != 0)
TabSetup(tab, id);
}
}
void TabSetup(ActionBar.Tab tab, int resID)
{
var relLay = new Android.Widget.RelativeLayout(activity)
{
LayoutParameters = new LayoutParams(LayoutParams.WrapContent, 180)
};
var linLay = new LinearLayout(activity)
{
LayoutParameters = new LayoutParams(LayoutParams.WrapContent, 180),
Orientation = Orientation.Vertical,
};
linLay.SetHorizontalGravity(Android.Views.GravityFlags.Center);
var imageView = new ImageView(activity);
imageView.SetImageResource(resID);
imageView.SetPadding(-35, 4, -35, 0);
imageView.SetMinimumWidth(60);
var textView = new TextView(activity)
{
Text = tab.Text
};
linLay.AddView(imageView);
linLay.AddView(textView);
relLay.AddView(linLay);
var badgeView = new TextView(activity)
{
Text = "2"
};
var badgeImageView = new ImageView(activity);
badgeImageView.SetImageResource(Resource.Drawable.red);
badgeImageView.SetMinimumWidth(5);
badgeImageView.SetMinimumHeight(5);
badgeImageView.SetPadding(77, 5, 0, 0);
badgeView.SetPadding(85, 0, 0, 0);
relLay.AddView(badgeImageView);
relLay.AddView(badgeView);
tab.SetCustomView(relLay);
}
int GetImageFromFilename(int n)
{
var filename = filenames[n].Split('.');
var id = Resources.GetIdentifier(filename[0], "drawable", activity.PackageName);
return id;
}
}
}
謝謝@Elvis夏 - MSFT,我改變了代碼,根據你的'建議'現在我得到'activity.SupportActionBar'的值,但activity.SupportActionBar。即使列表文件名包含「4」項目,「NavigationItemCount」也設置爲'0' –
Jamal
您已定義自己的「ActionBar」。這是一個全新的「ActionBar」。所以你需要自己添加項目。導航選項不會自動添加到它。 –
您能否提供任何樣本來添加我在* Home.cs *中可能包含的'Tab Pages',可能是'Burger.cs','Sandwich.cs','Pizza.cs','Roll.cs' – Jamal