底部导航视图(Android)的自定义形状?
如何使底部导航视图变为特定形状?
我想拥有以下形状的底部导航视图:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigationBottomView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_nav_bar"
app:itemHorizontalTranslationEnabled="true"
app:itemIconTint="@drawable/bottom_bar_selector"
app:itemTextColor="@drawable/bottom_bar_selector"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/nav_menu"/>
任何帮助将不胜感激。谢谢!
How to make the bottom navigation view to a specific shape?
I'd like to have a bottom navigation view of this shape:
I have tried setting it as background of my bottom nav view as:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigationBottomView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/bg_nav_bar"
app:itemHorizontalTranslationEnabled="true"
app:itemIconTint="@drawable/bottom_bar_selector"
app:itemTextColor="@drawable/bottom_bar_selector"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/nav_menu"/>
But it doesn't seem to work.
Any help will be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
bottomNavigationView
默认情况下具有材料ShapeDrawable
的背景,因此您可以通过定义自定义topedgeedgetReatment
shapeappearancemodel 更改其形状代码>在bottomNavigationView
上方绘制半圆。要能够在bottom navigationView
上绘制某些内容,您需要拥有一个具有以下属性的父:XML示例将如下:
然后绘制以下形状:
where
code> cutoutcircledgetTearment
是edgetReatment
的子类,在顶部绘制半圆,这是类似的代码从顶部到底部到底部:从上面的
cutoutcircledgetReatment
构造器您可以传递CircleDiameterDP
,它是DP值中的圆直径(在上面的示例中,将其设置为70DP,因此parent relativeLayout它的paddingtop等于圆的半径为70/2 = 35dp),并且circleleftrightofferoffsetdp
用于绘制DP值中的左/右偏置的圆圈。当然,您可以根据自己的需求进一步修改代码。结果:
以使半圆与托管的片段重叠,您必须更改
的顺序fragment:nav_host_fragment_activity_main
带有relativelayout bottomnavigationViewParentrl
在下面的示例中类似:在您的每个片段中也给出一个底部的底部边距,具有相同的高度,以便在SEMI的点开始,如下样品中的圆圈:
结果:
用法:
The
BottomNavigationView
by default has a background ofMaterialShapeDrawable
so you can change its shape using theShapeAppearanceModel
by defining a custom TopEdgeEdgeTreatment
to draw the half-circle above theBottomNavigationView
. To be able to draw something above theBottomNavigationView
you need to have a parent which has the below attributes:An Xml sample will be like the below:
Then draw the shape like the below:
where
CutoutCircleEdgeTreatment
is a subclass ofEdgeTreatment
to draw the half-circle at the top which is similar code like the build-inBottomAppBarTopEdgeTreatment
class which draws a semi-circular cutout from the top edge to bottom:From the above
CutoutCircleEdgeTreatment
constructor you can pass thecircleDiameterDp
which is the circle diameter in dp value (in the above example is set to 70dp so the parent RelativeLayout it should have paddingTop equal to the radius of the Circle which is 70/2 = 35dp) and thecircleLeftRightOffsetDp
is used to draw the circle with a left/right offset in dp value. Of Course you can modify further the code based on your needs.Result:
To overlap the semi circle with the fragment hosted
To make the semi circle overlap with the fragment hosted you have to change the order of
fragment:nav_host_fragment_activity_main
with theRelativeLayout bottomNavigationViewParentRL
like in the below sample:And also give in each of your fragments some bottom margin with the same height of the navigation bar to start at the point of semi circle like in the below sample:
Result:
Another variation of CutoutCircleEdgeTreatment
Usage:
Result: