【问题标题】:Best way to create a bottom toolbar in android.在 android 中创建底部工具栏的最佳方法。
【发布时间】:2015-07-15 19:18:00
【问题描述】:

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

【问题讨论】:

  • 有奖赏和名额的那个? ToolBar 是一个视图组,因此您可以将 imageButtons 放入其中并赋予它们重力
  • 是的。我尝试使用工具栏但无法获得类似的结果
  • 感谢 Elltz 我成功使用了您的方法。我必须使用带有可绘制顶部的按钮而不是 ImageButtons,并且还必须使用 toolbar.setContentInsetsAbsolute(0,0);正确设置内部视图。
  • 好吧,如果它有帮助,你可以投票,如果它确实解决了你想要的,你可以接受。 & 我很高兴你解决了它。

标签: android layout material-design


【解决方案1】:
<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() 或setnagivation,你也不必向它添加optionsMenu。所以它会像你想要的那样裸露。

试试看是否符合你的要求,记得把背景和图片src添加到ImageButtons

【讨论】:

  • 但是如何在语法上改变图像?请提供建议
  • 工具栏中的ImageButton?您可以为 ImageButton 添加 id 并通过 Toolbar.findViewById() 获取对它的引用,然后您可以在语法上更改 ImageButton 的图像(:- 我的意思是通过编程方式,希望对先生有所帮助
  • 是的,它很有帮助,我找到了解决方案
  • 你应该使用android:layout_weight="1" 而不是android:weight="1"
  • @vojta 是的,如果您希望LinearLayout 根据您将子项的宽度设置为0dp 的比率定位子项,其余的由父项完成。如果LinearLayout 具有垂直方向,并且您想要比例定位,那么您的高度将为 0dp,很酷吗?
【解决方案2】:

我知道这个问题已经很老了,但我认为现在为正在寻找相同事物的其他人回答永远不会太晚。我刚刚在该主题上找到了this tutorialthese official docs,现在底部导航已被 Android 正式支持,并且如果您的应用有 3 到 5 个顶级导航位置,它是材料设计指南的一部分。我希望这对其他人有所帮助,因为我花了很多时间四处寻找这个。

【讨论】:

  • 感谢您提供的链接,我知道 Android 支持底部工具栏,但我找不到它。
【解决方案3】:

我在 GitHub 上分享了我的解决方案。它适用于 Xamarin,但布局相同。

http://behsaadramez.com/2015/11/26/androidbottomtoolbar/

【讨论】:

    【解决方案4】:

    我曾经回来过这个问题。我无法在按钮下方添加文本。我写了一篇文章我是如何用代码sn-p和截图here, create goolge plus like toolbar in android解决问题的@

    【讨论】:

      【解决方案5】:

      这在 2020 年对我有用,Android Studio 3.5.3 是如何设置工具栏(已接受答案的后半部分)。 How do I align views at the bottom of the screen? 您可以在 androidx.appcompat.widget.Toolbar 中使用工具栏布局/小部件

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

      【讨论】:

        【解决方案6】:

        好吧,上次我尝试这样做时,我在布局底部使用了 Buttons,并由 LinearLayout 包裹,如下所示:

        <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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-02-19
          • 2017-09-26
          • 1970-01-01
          • 1970-01-01
          • 2018-09-27
          • 1970-01-01
          • 2020-04-18
          相关资源
          最近更新 更多