Xamarin Shell android 使用 xml 标记自定义

时间:2021-05-03 10:24:53

标签: c# xml xamarin xamarin.forms xamarin.android

我已经定义了自定义的底部视图来在 android 项目中制作自定义的 tabbar shell。 为此,我创建了一个新的渲染和一个新的 xml 文件来定义图形。 但是有一个问题,我有 margin 但底部视图下的背景是黑色的,我不知道如何编辑这种颜色。

这是我的代码:

public class MyShellBottomNavViewAppearanceTracker : IShellBottomNavViewAppearanceTracker
{
    public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
    {            
        bottomView.SetBackgroundResource(Resource.Drawable.bottomBack);                                  

        if (bottomView.LayoutParameters is LinearLayout.LayoutParams layoutParams)
        {
            layoutParams.SetMargins(30, 0, 30, 0);
            bottomView.LayoutParameters = layoutParams;
        }
    }
}

这是我的shell xml:

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <solid android:color="#fff555" />
  
  <corners android:topLeftRadius="15dp"
     android:topRightRadius="15dp"
           android:bottomLeftRadius="15dp"
     android:bottomRightRadius="15dp"
  />
  <padding android:top="5dp" android:bottom="5dp"/>
  
</shape>

结果显示为灰色背景,外壳为黄色,背景为黑色,但对于最后一个我不知道如何编辑(将灰色作为视图的其余部分)。

Screen about background tab bar is black and not gray as rest of the view

如何指定标签栏背景(超出边距)?

1 个答案:

答案 0 :(得分:0)

您可以尝试获取bottomView的Parent,然后将其背景颜色设置为灰色。

 public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
    {
       
        bottomView.SetBackgroundResource(Resource.Drawable.bottombackground);
        Android.Views.View view = (Android.Views.View)bottomView.Parent;
        view.SetBackgroundColor(Android.Graphics.Color.Gray);
        if (bottomView.LayoutParameters is LinearLayout.LayoutParams layoutParams)
        {
            layoutParams.SetMargins(30, 0, 30, 0);
            bottomView.LayoutParameters = layoutParams;
        }
    }
相关问题