【问题标题】:Android BottomAppBar Distribute Buttons horizontallyAndroid BottomAppBar 水平分布按钮
【发布时间】:2019-06-30 09:23:05
【问题描述】:

我想使用没有导航抽屉控件的 BottomAppBar 和 浮动操作按钮。添加按钮后,它们会向右或向左对齐。

  • 我想水平分布动作图标。我在 Android 开发人员 reference 周围和周围找不到解决方案。怎么可能做到这一点?

【问题讨论】:

  • BottomAppBar 与顶部工具栏相同。如果您想水平分布它们,请使用自定义视图或 BottomNavigationView。
  • 投反对票的人能写出原因吗?

标签: android android-layout material-design android-bottomappbar bottomappbar


【解决方案1】:

注意

如果您想删除 Navigation drawer control,只需从 BottomAppBar

中删除 app:navigationIcon="@drawable/ic_drawer"

output when your removed app:navigationIcon="@drawable/ic_drawer"

示例代码

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:layout_gravity="bottom">


    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:layout_gravity="bottom"
        android:backgroundTint="@color/colorPrimaryDark"
        app:navigationIcon="@drawable/ic_drawer">

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


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:src="@drawable/ic_favorite" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:src="@drawable/ic_favorite" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:src="@drawable/ic_favorite" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:src="@drawable/ic_favorite" />

        </LinearLayout>

    </com.google.android.material.bottomappbar.BottomAppBar>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

输出

【讨论】:

  • 添加 implementation 'com.google.android.material:material:1.0.0' 时 Gradle 失败。
  • Manifest Nerger failure.. org.gradle.execution.MultipleBuildFailures:构建完成,有 1 次失败
  • @kelalaka 与问题分享您的build.gradle
  • 好的。我需要 material.bottomappbar.BottomAppBar 还是 BottomAppBar 就够了?
  • @kelalaka 使用 com.google.android.material.bottomappbar.BottomAppBar
【解决方案2】:

通过删除线删除通知图标:

    app:navigationIcon="@drawable/ic_drawer"

来自

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="58dp"
    android:layout_gravity="bottom"
    android:backgroundTint="@color/colorPrimaryDark"
    app:navigationIcon="@drawable/ic_drawer">

然后就可以在bottomappbar中添加左右padding了:

<android.support.design.bottomappbar.BottomAppBar
        android:theme="@style/Widget.MaterialComponents.BottomAppBar"
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:paddingStart="40dp"
        android:paddingLeft="40dp"
        android:paddingEnd="40dp"
        android:paddingRight="40dp"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="center"/>

这将使底部AppBar 中间的四个图标居中。

最后一步是在图标/图像视图上添加内边距/边距,您应该拥有一个不错的甚至水平分布的四个图标。我认为接受的答案并没有完全实现这一点。

【讨论】:

  • 您能否在答案中添加屏幕截图?
猜你喜欢
  • 2013-05-12
  • 1970-01-01
  • 2021-12-10
  • 2015-08-11
  • 2014-05-29
  • 2013-02-06
  • 2011-08-26
  • 2017-04-05
  • 2013-07-19
相关资源
最近更新 更多