在安卓中创建底部工具栏的最佳方法。

15

示例工具栏

寻找一个好的方法来实现类似的工具栏。我应该使用图像按钮吗?


1
“有奖励和地方的那一个吗?ToolBar是一个ViewGroup,因此您可以将您的imageButtons放在其中,并为它们设置重力。” - Elltz
是的,我尝试使用工具栏,但无法达到类似的结果。 - Ved Agarwal
谢谢Elltz,我成功地使用了你的方法。我不得不使用带有可绘制顶部的按钮,而不是图像按钮,并且还必须使用toolbar.setContentInsetsAbsolute(0,0)来正确设置内部视图。 - Ved Agarwal
1
如果这对你有帮助,可以点赞;如果它解决了你想要的问题,你可以接受它。我很高兴你解决了它。 - Elltz
6个回答

12
<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/actionbarT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/thebackgroundimageyouwant"
android:minHeight="?attr/actionBarSize"
app:theme="@style/Base.Theme.AppCompat.CompactMenu" >

    <LinearLayout
        android:id="@+id/toolbarmenucontainer"
        android:layout_width="match_parent"
        android:weightSum="3"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/preferedbackground"
            android:clickable="true"
            android:scaleType="fitXY"
            android:layout_weight = "1"
            android:src="@drawable/preferredimage" />

         <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/preferedbackground"
            android:clickable="true"
            android:scaleType="fitXY"
            android:layout_weight = "1"
            android:src="@drawable/preferredimage" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/preferedbackground"
            android:clickable="true"
            android:scaleType="fitXY"
            android:layout_weight = "1"
            android:src="@drawable/preferredimage" />

    </LinearLayout>

</android.support.v7.widget.Toolbar>

这个想法是它们将像你想要的那样水平排列,然后不要在ToolBar上执行ToolBar.setTitle()或设置导航,也不需要添加optionsMenu。所以它将像你想要的那样简洁。

试试看是否符合你的要求,记得将背景和图像来源添加到ImageButton中。


但是如何在语法上更改图像呢?请提供建议。 - Anand Savjani
您的工具栏中有一个 ImageButton 吗?您可以为 ImageButton 添加 ID,并通过 Toolbar.findViewById() 获取对它的引用,然后可以通过编程方式更改 ImageButton 的图像(我的意思是编程方式,希望能够帮到您)。 - Elltz
是的,这很有帮助,我找到了解决方案。 - Anand Savjani
1
你应该使用 android:layout_weight="1" 而不是 android:weight="1" - florian-do
1
@vojta 如果你想让LinearLayout根据你设置的比例来定位子元素,那么你需要将子元素的宽度设置为“0dp”,然后父元素会自动完成剩下的工作。如果LinearLayout的方向是垂直的,并且你想要按比例定位,那么高度应该设置为0dp,明白了吗? - Elltz
显示剩余3条评论

6
我知道这个问题非常老,但我认为回答对于那些正在寻找同样内容的人来说永远不会太晚。现在 Android 官方支持底部导航栏,并且如果你的应用程序具有 3 到 5 个顶级导航位置,则它们是材料设计指南的一部分。我刚刚发现了这篇教程这些官方文档关于此主题。希望这能帮助其他人,因为我花了很多时间寻找这个。

2
谢谢提供链接,我知道Android支持底部工具栏,但是我找不到它。 - se22as

2

1

在2020年,适用于我使用的Android Studio 3.5.3版本中,设置Toolbar(接受答案的后半部分)的方法如下: 我该如何将视图对齐到屏幕底部? 您可以在androidx.appcompat.widget.Toolbar中使用Toolbar布局/小部件。

将alignParentBottom属性设置为“true”(将元素放置在RelativeLayout内部)。


1

0

上次我尝试做这个的时候,我使用了布局底部包裹着一个LinearLayoutButton,就像这样:

<LinearLayout>
// The other stuff on the view
(...)
</LinearLayout>
// (This is the part you can try to use as a toolbar)
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        style="?android:attr/borderlessButtonStyle"
        android:textStyle="bold"
        android:background="@drawable/button_tab"
        android:textColor="#ffffffff"
        android:text="@string/bt_orders"
        android:id="@+id/bt_orders"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        style="?android:attr/borderlessButtonStyle"
        android:textStyle="bold"
        android:background="@drawable/button_tab"
        android:textColor="#ffffffff"
        android:text="@string/bt_credit"
        android:id="@+id/bt_credit"
        android:layout_weight="1" />
</LinearLayout>

如果你对我使用的风格和背景感到好奇,这里是它们:

// button_tab.xml
<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/button_pressed" />
            <solid android:color="@color/button_pressed" />
        </shape>
    </item>
    <item android:state_focused="true" >
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/button_focus" />
            <solid android:color="@color/button_focus" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/button_normal" />
            <solid android:color="@color/button_normal" />
        </shape>
    </item>
</selector>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接