android - 如何在Android中居中&左&右对齐ActionBar图标

标签 android android-layout android-toolbar

您好,我想在工具栏中对齐一组图标,如下图所示。

我用谷歌搜索,经过一些改变,我有:

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>

enter image description here

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

根据图片,我将右侧图标 float 到左侧,将右侧图标 float 到右侧。

我也希望 float 中心所有其他图标并排在一起......

代码没有任何变化。

我还添加了 android:layout_centerVertical = "true"对所有其他图标...

怎样才能正确显示 View ?

最佳答案

这个答案可能有点晚了。但是,使用当今的 Material 组件库,您可以使用底部应用栏来实现您想要的结果。见伪代码 fragment :

<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"将用于表示您希望在底部应用栏中显示的图标。

您必须导入 Material 组件库才能使用底部应用栏

关于android - 如何在Android中居中&左&右对齐ActionBar图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31132007/

相关文章:

java - 使用 JSON 从字符串中解析数组?

android - Jetpack Compose 中的折叠布局

android - 添加两个具有不同主题的 AppCompat 工具栏

android - 向下滚动时,EnterAlwaysCollapsed 不会带回工具栏

android - 在 ViewFlipper 小部件中的各个元素上设置点击监听器

android - 为原生相机应用程序设置视频格式

java - 如何使用 Gson 解析 JSON 数组

Android:在翻译中缩放 EditText Android 问题并在放置在 parentView 外部时获取 childView 的 Touch 事件

android - (Robotium) 操作栏向上/主页按钮单击

list 中的 Android appcompat 工具栏标题