当我尝试在BottomBarBadge中显示大于99的计数时,由于徽章中没有足够的空间,因此无法正确显示。

此屏幕快照显示了一个徽章,实际上包含110个数字,但是看起来像“ 11”,因为小徽章将“ 0”截掉了:

android - Xamarin Android BottomBarBadge无法正确显示100+-LMLPHP

问题是,当计数改变时,我只是这样设置新的badge.Count

badge.Count = int.Parse(text);


在这一点上,如果徽章需要新的高度(例如,从99更改为100或以其他方式更改),我希望徽章可以自动呈现新的宽度以适应新的计数。但这似乎没有发生。

是否有我缺少的方法或其他东西来更新徽章的宽度?

我希望徽章可以在需要时在宽度上有所延伸,例如在WhatsApp中:

android - Xamarin Android BottomBarBadge无法正确显示100+-LMLPHP

我正在使用由pocheshire提供的3rd Party BottomNavigationBar:https://github.com/pocheshire/BottomNavigationBar



编辑:

使用Sushi Hangover's answer确实有效,但它又破坏了其他功能。当我点击包含徽章的标签时,该应用程序将在NullReferenceException中崩溃:

System.NullReferenceException: Object reference not set to an instance of an object.
  at BottomNavigationBar.BottomBar.HandleClick (Android.Views.View v) [0x00010] in <f60603cf39c84bebb4c6ba69e7e8bb64>:0
  at BottomNavigationBar.BottomBar+<MakeBadgeForTabAt>c__AnonStorey1.<>m__0 () [0x00011] in <f60603cf39c84bebb4c6ba69e7e8bb64>:0
  at BottomNavigationBar.Listeners.OnTabClickListener.OnClick (Android.Views.View v) [0x0000d] in <f60603cf39c84bebb4c6ba69e7e8bb64>:0
  at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v) [0x0000f] in <263adecfa58f4c449f1ff56156d886fd>:0
  at (wrapper dynamic-method) System.Object.30ced559-6971-4697-bb8d-82a961a4b1e9(intptr,intptr,intptr)


我在Custom Renderer中找不到错误,我在调用MakeBadgeForTabAt()的每行代码中都设置了断点,但是在异常发生之前它没有执行此代码。

这是似乎导致异常的代码:

protected virtual void OnTabbedPagePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    var page = sender as Page;
    if (page == null)
        return;

    if (e.PropertyName == TabBadge.BadgeTextProperty.PropertyName)
    {
        if (CheckValidTabIndex(page, out int tabIndex))
        {
            var element = Element.Children[tabIndex];
            UpdateTabBadgeText(BadgeViews[element], page);
        }
        return;
    }
}

private void UpdateTabBadgeText(BottomBarBadge badge, Element element)
{
    var text = TabBadge.GetBadgeText(element);
    if (!String.IsNullOrEmpty(text))
    {
        try
        {
            // This does not cause an exception, but doesn't update the badge's size:
            // badge.Count = int.Parse(text);

            // This causes NullReferenceException when taping the tab:
            var index = Element.Children.IndexOf((Page)element);
            _bottomBar.RemoveBadgeAt(index);
            _bottomBar.MakeBadgeForTabAt(index, "#FF0000", int.Parse(text));
        }
        catch (Exception)
        {
            // exception handling
        }
    }
    if (badge.Count == 0)
    {
        badge.Hide(false);
    }
    else
    {
        badge.Show(false);
    }
}

最佳答案

那是不推荐使用的Java版本的移植版本,该问题出在原始版本中。

一种解决方法是,当值超过99时删除徽章,然后再次添加徽章,而不仅仅是增加计数。

我没有看过C#版本,但是假设BottomBarBadge.java的代码是“相同”的,并且徽章视图的大小在attachToTab方法期间仅计算一次。因此,解决方法有效,因为再次调用了attachToTab/adjustPositionAndSize流,但是应该真正检查徽章内容,以确定它们是否会使徽章圈溢出并重新调用adjustPositionAndSize,...我认为...;- )

回复:https://github.com/roughike/BottomBar/blob/master/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarBadge.java#L94

更新:

该库中有很多问题/错误,包括删除/重新创建徽章等。(与原始Java库略有不同)。

虽然这应该在库本身中,但是您可以在设置计数时执行以下操作以正确调整徽标的大小:

void ResetBadgeCount(BottomBarBadge badge, int count)
{
    badge.Count = count;
    ViewGroup.LayoutParams lparams;
    using (var bounds = new Rect())
    {
        badge.Paint.GetTextBounds(badge.Text, 0, badge.Text.Length, bounds);
        lparams = _badge2.LayoutParameters;
        badge.SetSingleLine();
        lparams.Width = (int)((bounds.Right - bounds.Left) * 1.25);
    }
    lparams.Height = lparams.Width;
    badge.LayoutParameters = lparams;
}


并在每次设置徽章计数时调用它:

ResetBadgeCount(_yourBadgeInstance, 999);


或使其成为扩展方法:

public static class MyExtensions
{
    public static void ResetBadgeCount(this BottomBarBadge badge, int count)
    {
        badge.Count = count;
        ViewGroup.LayoutParams lparams;
        using (Rect bounds = new Rect())
        {
            badge.Paint.GetTextBounds(badge.Text, 0, badge.Text.Length, bounds);
            badge.SetSingleLine();
            lparams = badge.LayoutParameters;
            lparams.Width = (int)((bounds.Right - bounds.Left) * 1.25);
        }
        lparams.Height = lparams.Width;
        badge.LayoutParameters = lparams;
    }
}


然后,您可以通过以下方式调用它:

_yourBadgeInstance.ResetBadgeCount(999);
_yourBadgeInstance.ResetBadgeCount(1999);




要获得更像“ WhatsApp式”的外观,请在问题中的图像中查找徽章(拉伸其宽度,但不增加高度),可以在ResetBadgeCount方法中使用以下代码:

private void ResetBadgeCount(BottomBarBadge badge, int count)
{
    badge.Count = count;
    ViewGroup.LayoutParams lparams;
    using (var bounds = new Rect())
    {
        badge.Paint.GetTextBounds(badge.Text, 0, badge.Text.Length, bounds);
        lparams = badge.LayoutParameters;
        badge.SetSingleLine();
        lparams.Width = (int)Math.Round(badge.Paint.MeasureText(badge.Text) * 1.25);
        var metrics = badge.Paint.GetFontMetrics();
        lparams.Height = (int)Math.Round((metrics.Bottom - metrics.Top) * 1.25);
    }
    if (lparams.Width < lparams.Height)
    {
        lparams.Width = lparams.Height;
    }
    badge.LayoutParameters = lparams;
}

关于android - Xamarin Android BottomBarBadge无法正确显示100+,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50675807/

10-11 20:26