在自定义导航中,我使用以下方法获取要在iOS部分中导航的图像背景:
NavigationBar.BarTintColor = UIColor.FromPatternImage(UIImage.FromFile("my_image"));
成功。
如何在我的android自定义类上获得相同的信息?现在我只能在android上使用背景色:
MyCustomNavigation.BarBackgroundColor = Color.FromHex("#COD_HEX");
(请注意,我仅需要针对某些视图使用自定义图片,而不需要全局使用)
答案 0 :(得分:1)
如果您想为导航栏自定义图像,我为此问题编写了一个简单的演示。您可以参考它。
首先,您可以创建一个扩展NavigationPage的新类,并将图像复制到Android项目中的“ drawable”文件夹中。
public class MyCustomNavigation : NavigationPage
{
public MyCustomNavigation(Page page) : base(page)
{
}
public static readonly BindableProperty BarBackgroundProperty = BindableProperty.CreateAttached(
"BarBackground",
typeof(string),
typeof(MyCustomNavigation), string.Empty);
public static string GetBarBackground(BindableObject view)
{
return (string)view.GetValue(BarBackgroundProperty);
}
public static void SetBarBackground(BindableObject view, string value)
{
view.SetValue(BarBackgroundProperty, value);
}
}
然后,您可以创建一个设置导航页面背景的内容页面。
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BarBackgroundPage : ContentPage
{
public BarBackgroundPage ()
{
InitializeComponent();
Title = "Monkey Title";
MyCustomNavigation.SetBarBackground(this, "monkeybackground");
}
}
最后,您可以在Android项目的Renderers文件夹中创建一个类CustomNavigationPageRenderer。
[assembly: ExportRenderer(typeof(MyCustomNavigation), typeof(CustomNavigationPageRenderer))]
namespace App4.Droid.Renderers
{
public class CustomNavigationPageRenderer : NavigationPageRenderer
{
Android.Support.V7.Widget.Toolbar _toolbar;
Drawable _originalToolbarBackground;
Drawable _originalWindowContent;
Android.Widget.FrameLayout _parentLayout;
public CustomNavigationPageRenderer(Context context) : base(context)
{
}
protected override void SetupPageTransition(Android.Support.V4.App.FragmentTransaction transaction, bool isPush)
{
Page lastPage = null;
if (isPush)
{
lastPage = Element?.Navigation?.NavigationStack?.Last();
lastPage.PropertyChanged += LastPage_PropertyChanged;
}
}
private void LastPage_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var lastPage = sender as Page;
UpdateToolbarBackground(_toolbar, lastPage, Context as Activity, _originalToolbarBackground);
}
private void UpdateToolbarBackground(Android.Support.V7.Widget.Toolbar toolbar, Page lastPage, Activity activity, Drawable originalToolbarBackground)
{
string Picname = MyCustomNavigation.GetBarBackground(lastPage);
int resid = this.Context.Resources.GetIdentifier(Picname, "drawable", Android.App.Application.Context.PackageName);
toolbar.SetBackgroundResource(resid);
}
public override void OnViewAdded(Android.Views.View child)
{
base.OnViewAdded(child);
if(child.GetType()==typeof(Android.Support.V7.Widget.Toolbar))
{
var lastPage = Element?.Navigation?.NavigationStack?.Last();
_toolbar = (Android.Support.V7.Widget.Toolbar)child;
_originalToolbarBackground = _toolbar.Background;
var originalContent = (Context as Activity)?.Window?.DecorView?.FindViewById<FrameLayout>(Window.IdAndroidContent);
if (originalContent != null)
{
_originalWindowContent = originalContent.Foreground;
}
_parentLayout = new Android.Widget.FrameLayout(_toolbar.Context)
{
LayoutParameters = new Android.Widget.FrameLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent)
};
}
}
}
}
如果您想了解有关服装导航的更多详细信息,可以打开this link并下载项目。