微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何删除导航图标菜单中的填充

如何解决如何删除导航图标菜单中的填充

我正在使用导航组件 Jetpack,所以我在我的工具栏中自动生成了一些按钮。像这样:

pic1

但是结果太令人失望了,代表 Burgerbutton 的 AppcompatimageButton 占用了太大的填充,如下所示:

pic2

我试过这样的东西,但没有任何效果

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:contentInsetStartWithNavigation="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetEnd="0dp"
            app:contentInsetRight="0dp"
            app:popupTheme="@style/AppTheme.PopupOverlay">

<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="5">
</LinearLayout>

</androidx.appcompat.widget.Toolbar>

如何减少填充大小?

解决方法

你可以做的是为工具栏制作一个单独的布局,并在那里添加一个 Burgerbutton 的图像,并为其设置所需的填充或边距。

    <androidx.appcompat.widget.Toolbar
    android:layout_width="0dp"
    android:layout_height="match_parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@color/appWhiteColor"
    app:contentInsetStart="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent">

            <ImageView
                android:id="@+id/btnBurger"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:background="@drawable/nav_icon"
                android:layout_marginStart="@dimen/_5sdp"
                android:layout_marginEnd="@dimen/_2sdp"
                android:layout_marginTop="@dimen/_13sdp"
                android:layout_marginBottom="@dimen/_8sdp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="@id/guidelinev2">
            </ImageView>

            <TextView
                android:id="@+id/textView1"
                android:layout_height="0dp"
                android:layout_width="0dp"
                android:text="Orders"
                android:layout_marginEnd="@dimen/_20sdp"
                android:gravity="center"
                android:visibility="visible"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
                android:fontFamily="@font/comfortaa_regular"
                android:textStyle="bold"
                android:textColor="@color/black"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="@id/guidelinev2"
                app:layout_constraintEnd_toEndOf="parent">
            </TextView>

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guidelinev1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.6"/>

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guidelinev3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.85"/>

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guidelinev2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.1"/>

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guidelineh1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.15"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>
</androidx.appcompat.widget.Toolbar>

现在把它放在你的 navigationDrawerActivity 中

      ImageView expand = findViewById(R.id.btnBurger);
      expand.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawer.openDrawer(GravityCompat.START);
        }
    });

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。