【问题标题】:How to remove padding in Navigation Icon Menu如何删除导航图标菜单中的填充
【发布时间】:2021-02-10 03:00:48
【问题描述】:

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

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

我试过这样的方法,但是没有任何效果

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

如何减小填充大小?

【问题讨论】:

  • 这看起来已经是 48x48dp 的最小可访问触摸尺寸了。
  • 你的意思是我不能再减少填充了吗?

标签: android toolbar android-appbarlayout android-jetpack-navigation


【解决方案1】:

您可以做的是为工具栏制作一个单独的布局,并在其中添加一个 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);
        }
    });

【讨论】:

  • 实际上我需要 AppCompatImageButton 以便在 NavigationComponent 中的片段之间转换时使用它们的动画。如果我改变它,我可能会失去它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
相关资源
最近更新 更多