【问题标题】:Shell TabBar rounded corners override default background color (View) behindShell TabBar 圆角覆盖默认背景色(View)后面
【发布时间】:2021-07-08 07:03:03
【问题描述】:

我在 Xamarin Forms Shell TabBar Rounded Corner 中的 Shell TabBar 上应用圆角。

我的问题:是否可以将视图(背景颜色)放在后面而不是上面(默认黑色)?

【问题讨论】:

  • 你确定你链接的问题是关于设置圆角吗?
  • 哎呀,错了。我编辑了。

标签: c# xamarin.forms custom-renderer xamarin.forms.shell


【解决方案1】:

您可以将父级的BackgroundColor 设置为当前的ContentPage BackgroundColorContent(可能是一个布局)BackgroundColor

    public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
        {
            var currentContentPage = (Shell.Current.CurrentPage as ContentPage);
            if (currentContentPage == null)
            {
                return;
            }

            if (currentContentPage.Content != null && currentContentPage.Content.BackgroundColor != Color.Transparent)
            {
                (bottomView.Parent as LinearLayout)?.SetBackgroundColor(currentContentPage.Content.BackgroundColor.ToAndroid());
            }
            else
            {
                (bottomView.Parent as LinearLayout)?.SetBackgroundColor(currentContentPage.BackgroundColor.ToAndroid());
            }

            bottomView.SetBackgroundResource(Resource.Drawable.bottombackground);
        }

注意

由于是后续问题,我只放了针对这个问题的相关代码,完整代码可以在https://stackoverflow.com/a/65784730中找到

【讨论】:

  • 如果它回答了您的问题或对您有帮助,请通过an upvote ▲/✅ 告诉我,否则您可以给我留下反馈/评论。
【解决方案2】:

您可以按如下方式定义可绘制背景:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="#f00" />
            <corners android:topLeftRadius="20dp" android:topRightRadius="20dp" />
        </shape>
    </item>
</layer-list>

另外,您可以将 Elevation 属性设置为 0 以防止出现阴影:

bottomView.SetBackgroundResource(Resource.Drawable.bottombackground);
bottomView.Elevation = 0;

【讨论】:

  • 这个解决方案的问题是&lt;solid android:color="#ffffff"是硬编码的,所以如果他设置他的一个ContentPage的BackgroundColor,或者将StackLayout设置为Content并给它一个BackgroundColor你会发现你自己在同一个问题的问题上。
  • 是的,它是硬编码的,但是当视图中的背景颜色相同时,它是一个有效的解决方案。我正在尝试使用相同的可绘制背景文件。
  • 同意这是一个有效的解决方案我刚刚指出了这种情况,因为我自己在最初对颜色进行了硬编码,然后才发现这在扫描仪中可能不方便。
猜你喜欢
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 2021-09-19
  • 2013-02-09
  • 2021-11-10
  • 1970-01-01
相关资源
最近更新 更多