之前一直都是用android.support.v7.widget.Toolbar

换了台电脑安装Androidstudio之后就莫名其妙的用不了,比如设置app:navigationIcon=""这个是设置左上角按钮或者图标的,我就很迷怎么设置都搞不好,后来我就想了个奇特的点子

先看效果
在这里插入图片描述
有点搞,这个其实并不是原生的菜单按键,我换成了图片而已,为了实现文字居中,我又在左边加了个空的imageview

在这里插入图片描述

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="?attr/actionBarTheme">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:padding="7dp" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="@string/news_item"
                    android:textColor="@color/white"
                    android:textSize="18dp" />


                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:padding="7dp"
                    android:src="@drawable/ic_add" />


            </LinearLayout>


        </androidx.appcompat.widget.Toolbar>
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐