Custom Renderer to Change Icon Position on NavigationBar Xamarin Forms

时间:2018-01-11 08:35:58

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

enter image description hereI already create Custom Renderer to add Logo in my Navigation Bar but the problem is The Logo is on the left side of the Title, what I want is the Logo will be on the Right Side of the Title here is my Custom Renderer to do that

  protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
        {
            base.OnElementChanged(e);
            var bar = (Android.Support.V7.Widget.Toolbar)typeof(NavigationPageRenderer)
                    .GetField("_toolbar", BindingFlags.NonPublic | BindingFlags.Instance)
                    .GetValue(this);
            //  bar.SetNavigationIcon(Resource.Drawable.ic_myvalue);

            bar.SetLogo(Resource.Drawable.ic_myvalue);


        } 

any suggestions to do that? or can we do that? thanks

and here is my CustomToolbar.axml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light">
  <ImageView
      android:id="@+id/imageViewToolbar"
      android:layout_width="100dp"
      android:src="@drawable/ic_myvalue"
      android:layout_height="match_parent"
      android:layout_marginLeft="20px"/>

</android.support.v7.widget.Toolbar>

1 个答案:

答案 0 :(得分:1)

这是你想要的吗?

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <TextView
                android:text="Title"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"/>
            <ImageView
                android:src="@drawable/icon"
                android:id="@+id/imageViewToolbar"
                android:layout_width="100dp"
                android:layout_height="match_parent" />
        </LinearLayout>
    </android.support.v7.widget.Toolbar>