首页 > 解决方案 > 如何在工具栏菜单中居中(水平)图标

问题描述

如何在工具栏菜单中将图标居中,我尝试添加填充或边距但不起作用。如何在工具栏菜单中将图标居中,我尝试添加填充或边距但不起作用。

这是代码,如果我需要上传更多源代码,请告诉我。 在此处输入图像描述 主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <item
        android:id="@+id/item1"
        android:icon="@drawable/ic_credit_card_black_24dp"
        android:title=" "
        app:showAsAction="ifRoom"/>
    <item
        android:id="@+id/item2"
        android:icon="@drawable/ic_credit_card_black_24dp"
        android:title=" "
        app:showAsAction="ifRoom"/>

</menu>

menu_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">



    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:layout_height="wrap_content">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/colappsingtoolbar"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            app:layout_scrollFlags="exitUntilCollapsed|scroll"
            app:contentScrim="?attr/colorPrimary"
            app:title=" "
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp" >
            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/mainimg"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

标签: androidandroid-studioandroid-layout

解决方案


我不知道是否存在一种简单的方法,但您可以创建 CustomToolbar。例如,创建一个名称为:toolbar 的 xml 布局

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/tool_bar_size_mio"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:weightSum="10">
    <Button
            android:id="@+id/btn_menu"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button"/>
</LinearLayout>

然后,包括到您的活动 xml

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar_menu_principal" />

推荐阅读