【问题标题】:how to align toolbaritems in xamarin forms for cross platform如何为跨平台对齐 xamarin 表单中的工具栏项
【发布时间】:2020-10-02 16:27:23
【问题描述】:

例如

<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>

将在工具栏的右侧显示所有项目...我想在中间显示徽标,然后在右侧显示购物车图像和购物车中的编号标签...

在线阅读许多有关创建客户渲染的示例...(Xamarin forms position of toolbar items for android)

现在还是这样吗?正如几年前问过这些问题... xamarin 是否已更新以包括工具栏中项目的对齐方式,或者我是否需要创建自定义渲染...请告知谢谢

【问题讨论】:

    标签: xamarin.forms xamarin.android xamarin.ios cross-platform toolbaritems


    【解决方案1】:

    这个问题(不能在xamarin表单代码中设置图标居中)刚刚发生在Android,您不需要在Android中创建自定义渲染器。

    首先,请打开 Resources/layout 文件夹中的 Toolbar.xml 文件,这是您在 MainActivity 中设置的内容,Xamarin.Forms 将使用它来膨胀它。将ImageView 添加到android.support.v7.widget.Toolbar,如下代码所示。

    <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>
    

    这是运行截图。

    这是一个有用的链接,你可以参考一下。

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

    【讨论】:

    • 啊,非常感谢您的帮助...唯一的问题是我的观众将是 ios 和 andriod...所以从长远来看,创建自定义渲染可能更可行...现在还有更多工作,但正如我所说,我也需要 ios
    • 你可以在答案中看到链接,它有关于iOS的解决方案
    • 如果答案有帮助,请采纳为答案,对有类似问题的其他人有帮助
    • 如果你使用自定义渲染器,你需要获取action bar然后创建一个imageview,将imageView设置为action bar并将布局设置在中心。我试过这种方式,但是action bar总是为null,我们需要在main Activity中设置一个新的action,这太复杂了。
    • 非常感谢您的帮助。都在链接中
    猜你喜欢
    • 2017-12-28
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    相关资源
    最近更新 更多