PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 13, 2022

[FIXED] how to align toolbaritems in xamarin forms for cross platform

 September 13, 2022     cross-platform, toolbaritems, xamarin.android, xamarin.forms, xamarin.ios     No comments   

Issue

so for example

<ContentPage.ToolbarItems>
    <ToolbarItem Name="logo" Icon="businessName.png" Priority="0" Order="Primary"/>
    <ToolbarItem Name="shoppingCartImage" Icon="shopping_cart_IMG.png" Priority="0" Order="Primary"/> Activated="Onclick" />
    <ToolbarItem x:Name="NumberOfItemsInCart" Priority="0" Order="Primary"/>
</ContentPage.ToolbarItems>

will display all items to the right of the toolbar...I would like the logo in the centre and then shopping cart image and label for number in shopping cart to the right...

Read many examples online regarding creating a customer render...(Xamarin forms position of toolbar items for android)

is this still the case? as these Q's were asked years ago...has xamarin been updated to include alignment of items in the toolbar or do I need to create a custom render... please advise thank you


Solution

This issue(cannot set the icon to center in xamarin forms code) just happened in Android, you do not need to create a custom renderer in Android.

First of all, please open the Toolbar.xml file in Resources/layout folder,this is what you set in the MainActivity, which Xamarin.Forms will use to inflate it. Add a ImageView to the android.support.v7.widget.Toolbar like following code.

<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:contentDescription="businessname icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:scaleType="fitCenter"
        android:src="@drawable/businessname"
        android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>

Here is running screenshot.

enter image description here

Here is a helpful link, you can refer to it.

https://montemagno.com/xamarin-forms-icons-in-navigation-toolbar/



Answered By - Leon Lu - MSFT
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing