问题描述
我正在尝试在我的底部导航中添加圆角,并且尝试更改宽度和高度,但是它不起作用。我正在使用相对布局,其宽度设置为“ fill_parent”,高度设置为“ warp_content”,我有两个图标登录并注册,并且我希望整个导航都具有圆角。我正在使用Material Design底部导航:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/botttom_navigation"
app:itemBackground="@android:color/darker_gray"
app:itemTextColor="@drawable/selector"
app:itemIconTint="@drawable/selector"
android:layout_alignParentBottom="true"
app:menu="@menu/menu_navigation"/>
这是它的样子
解决方法
最简单的方法是将BottomNavigationView包裹在CardView中,并将半径设置为所需的值。
这是一个例子:
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:itemIconTint="@drawable/bottom_nav_colors"
app:itemTextColor="@drawable/bottom_nav_colors"
app:menu="@menu/menu_nav_bottom" />
</androidx.cardview.widget.CardView>
结果如下:
,正如您在问题中提到的,如果您不寻找阴影。 您可以使用简单的 Shape Drawable 来实现它,无需为此结果添加额外的库:
在您的活动/片段XML中:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/botttom_navigation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="@dimen/spacing_40"
android:background="@drawable/nav_background_curved"
app:itemIconTint="@color/c_white"
app:itemTextColor="@color/c_white"
app:menu="@menu/dashboard_menu" />
</RelativeLayout>
在名为nav_background_curved的可绘制文件夹中创建XML
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#1bc0a7" />
<corners android:radius="10dp" />
</shape>
,
BottomNavigationView
使用 MaterialShapeDrawable
作为背景。
您可以使用类似的内容:
<com.google.android.material.bottomnavigation.BottomNavigationView
app:backgroundTint="....."
android:id="@+id/botttom_navigation"
app:menu="@menu/...."/>
,然后应用带有圆角的 shapeAppearanceModel
:
val bottomNavigationViewation : BottomNavigationView = findViewById(R.id.botttom_navigation)
val radius = resources.getDimension(R.dimen.cornerSize)
val shapeDrawable : MaterialShapeDrawable= bottomNavigationViewation.background as MaterialShapeDrawable
shapeDrawable.shapeAppearanceModel = shapeDrawable.shapeAppearanceModel
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED,radius)
.build()