如何在Android中居中对齐ActionBar图标

问题描述 投票:10回答:1

嗨,我想在工具栏中对齐图标,如下图所示。

我用Google搜索,经过一些更改后,我拥有:

mainActivity.xml

...

<include
    android:id="@+id/tool_bar_bottom"
    layout="@layout/tool_bar"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />
 ....

并且在menu_bottom.xml中:

<menu 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" tools:context=".MainActivity">

    <item
        android:id="@+id/action_forward"

        android:title="forward"
        android:orderInCategory="100"
        android:icon="@drawable/ic_action_arrow_forward"
        android:layout_alignParentRight="true"
        app:showAsAction="always"
        ></item>


    <item
        android:id="@+id/action_zoom_in"
        android:orderInCategory="200"
        android:title="zoom in"
        android:icon="@drawable/ic_action_zoom_in"
        app:showAsAction="always"
        ></item>



    <item
        android:id="@+id/action_home"
        android:orderInCategory="300"
        android:title="home"
        android:icon="@drawable/ic_action_home"
        app:showAsAction="always"
        ></item>


    <item
        android:id="@+id/action_refresh"
        android:orderInCategory="400"
        android:title="refresh"
        android:icon="@drawable/ic_action_refresh"
        app:showAsAction="always"
        ></item>

    <item
        android:id="@+id/action_zoom_out"
        android:orderInCategory="500"
        android:title="zoom out"
        android:icon="@drawable/ic_action_zoom_out"
        app:showAsAction="always"
        ></item>

    <item
        android:id="@+id/action_back"
        android:orderInCategory="600"
        android:title="back"
        android:icon="@drawable/ic_action_arrow_back"
        android:layout_alignParentLeft="true"
        app:showAsAction="always"
        ></item>


</menu>

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9uQTh1YS5qcGcifQ==” alt =“在此处输入图像描述”>

我也为左右添加了android:layout_alignParentLeft/right="true"

根据图片,我将向右浮动图标向右浮动,向右浮动图标向右浮动。

我也想使所有其他图标彼此中间居中...

代码没有任何变化。

也将android:layout_centerVertical = "true"添加到所有其他图标...

如何纠正显示视图?

android android-layout android-toolbar
1个回答
0
投票

此答案可能有点晚。但是,使用今天的当前材料组件库,您可以使用底部的应用程序栏来获得所需的结果。参见伪代码片段:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:backgroundTint="@color/colorBottomBar"
        app:fabAlignmentMode="center"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1
                android:drawableTop="@drawable/ic_blur"
                android:gravity="center"

            </TextView>

            <TextView
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1
                android:drawableTop="@drawable/ic_blur"
                android:gravity="center"

            </TextView>

            <TextView
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1
                android:drawableTop="@drawable/ic_blur"
                android:gravity="center"

            </TextView>

            <TextView
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1
                android:drawableTop="@drawable/ic_blur"
                android:gravity="center"

            </TextView>

            <TextView
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1
                android:drawableTop="@drawable/ic_blur"
                android:gravity="center"

            </TextView>

            <TextView
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1
                android:drawableTop="@drawable/ic_blur"
                android:gravity="center"

            </TextView>
        </LinearLayout>
    </com.google.android.material.bottomappbar.BottomAppBar>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

android:drawableTop =“ @ drawable / ic_blur”将用于表示要显示在底部应用栏中的图标。

您必须导入材料成分库才能使用底部的应用程序栏

© www.soinside.com 2019 - 2024. All rights reserved.