【问题标题】:How to align items in an android-toolbar?如何对齐 android-toolbar 中的项目?
【发布时间】:2016-12-10 17:30:41
【问题描述】:

如何将工具栏的项目左、中、右对齐?每次我从toolbar_menu.xml 为菜单充气时,它都会将图标加载到最右边。我怎样才能把它们按我想要的顺序排列?我正在使用屏幕底部的独立工具栏。

我有一个包含工具栏的 AXML。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbarMenuMain"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="#CC2827"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:layout_alignParentBottom="true"/>

我的主要活动:

    mToolbarMenu = FindViewById<SupportToolbar> (Resource.Id.toolbarMenuMain);        
    mToolbarMenu.InflateMenu(Resource.Menu.toolbarMenu);
    mToolbarMenu.MenuItemClick += mToolbarMenu_MenuItemClick;

还有toolbar_menu.xml。

<?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:myapp="http://schemas.android.com/apk/res-auto">
       <item android:id="@+id/action_home"
             android:icon="@drawable/ic_directions_car_white_24dp"
             android:title="Home"
             android:layout_gravity="left"
             myapp:showAsAction="always"/>
       <item android:id="@+id/action_Shop"
             android:icon="@drawable/ic_store_mall_directory_white_24dp"
             android:title="Shop"
             android:layout_gravity="center"
             myapp:showAsAction="always"/>
       <item android:id="@+id/action_Map"
             android:icon="@drawable/ic_map_white_24dp"
             android:title="Maps"
             android:layout_gravity="right"
             myapp:showAsAction="always"/>
    </menu>

【问题讨论】:

    标签: android visual-studio xamarin.android alignment android-toolbar


    【解决方案1】:

    layoutDirection 属性应该添加到工具栏标签中。 例如:

    android:layoutDirection="rtl"
    

    【讨论】:

      【解决方案2】:

      如果要将工具栏的项目左、中、右对齐,则需要为其创建单独的布局。因为在菜单中您没有任何更改位置的选项,它们将始终出现在屏幕右侧。您可以使用自定义布局并在工具栏中对其进行扩充。 请参考-Add custom layout to toolbar

      【讨论】:

        猜你喜欢
        • 2020-06-12
        • 1970-01-01
        • 2016-04-16
        • 2014-05-26
        • 2020-07-13
        • 2016-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多