Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中创建底部工具栏的最佳方法。_Android_Layout_Material Design - Fatal编程技术网

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

在android中创建底部工具栏的最佳方法。,android,layout,material-design,Android,Layout,Material Design,寻找一个很好的方法来实现类似的工具栏。我应该使用图像按钮吗 <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" andro

寻找一个很好的方法来实现类似的工具栏。我应该使用图像按钮吗
<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.setTitle()
或setnagivation,也不必在
工具栏上添加选项菜单。所以它会像你想要的一样裸露


试试看它是否符合您的要求,记住将背景和图像src添加到
ImageButton
s

好吧,上次我试着这么做时,我在布局底部使用了
按钮
s,由
线性布局
包裹,类似这样:

<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>
//button\u tab.xml

我在GitHub上共享了我的解决方案。这是针对Xamarin的,但布局是相同的


有一天我问了这个问题。我无法在按钮下方添加文本。我写过一篇文章,是如何用代码片段和屏幕截图解决这个问题的。我知道这个问题太老了,但我想,为了其他正在搜索相同内容的人,回答这个问题永远都不晚。我刚刚发现,在这个问题上,如果你的应用程序有3到5个顶级导航位置,那么底部导航是Android官方支持的,并且是材料设计指南的一部分。我希望这对其他人有帮助,因为我花了很多时间四处寻找

这对我来说在2020年是可行的,Android Studio 3.5.3是如何设置工具栏(接受答案的后半部分)。 您可以在androidx.appcompat.widget.Toolbar中使用工具栏布局/小部件

设定 对齐底部
属性设置为“true”(将元素放在RelativeLayout中)。

带有奖励和角色的元素?工具栏是一个视图组,所以您可以将图像按钮放在其中,并赋予它们重力。我尝试过使用工具栏,但没有达到类似的效果。汉克斯·埃尔茨:我成功地使用了你的方法。我必须使用可绘制顶部的按钮,而不是图像按钮,还必须使用工具栏;正确设置内部视图。如果它有帮助,您可以向上投票,如果它确实解决了您想要的问题,您可以接受我很高兴你解决了这个问题。但是如何从语法上改变图像呢?请在工具栏中的
ImageButton
中提供建议?您可以为
ImageButton
添加id,并通过
工具栏获得对它的引用。findViewById()
然后您可以更改
ImageButton的图像,语法上(:-我的意思是以编程方式,希望对sirYeah有所帮助,我找到了解决方案您应该使用
android:layou weight=“1”
而不是
android:weight=“1”
@vojta yes如果你想让
线性布局
根据比率定位孩子,你可以将孩子的宽度设置为
0dp
,剩下的由家长来做。如果
线性布局
的方向是垂直的,而你想让比率定位,那么你的身高将是0dp,酷吗?谢谢你的链接,我知道了droid支持底部工具栏,但我很难找到它。