在BottomNavigationMenu中的圆角菜单项

huangapple go评论76阅读模式
英文:

Rounded menu item in BottomNavigationMenu

问题

把BottomNavigation菜单中的每个菜单项都做成圆角,附有示例菜单图片。这样做是否可行?

英文:

I want to have rounded corners for each menu item in BottomNavigation menu. Sample menu image is attached. Is it possible?

在BottomNavigationMenu中的圆角菜单项

答案1

得分: 1

最终,我找到了解决我的问题的方法。

1)在 bg_prog_round.xml 文件中添加以下内容:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    
    <solid android:color="@color/edit_text_back" />
    
    <stroke
        android:width="0dp"
        android:color="@color/edit_text_back" />
    
    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />
    
</shape>

2)创建另一个 XML 文件并添加以下内容(bg_prog_orange_round.xml):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    
    <solid android:color="@color/orange" />
    
    <stroke
        android:width="2dp"
        android:color="@color/orange" />
    
    <corners
        android:bottomLeftRadius="@dimen/_15sdp"
        android:bottomRightRadius="@dimen/_15sdp"
        android:topLeftRadius="@dimen/_15sdp"
        android:topRightRadius="@dimen/_15sdp" />
    
</shape>

3)创建 bottom_back_nav.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_prog_orange_round" android:state_checked="true" />
    <item android:drawable="@drawable/bg_prog_round" />
</selector>

4)将以下内容添加到你的 BottomNavigationView 中 -> app:itemBackground="@drawable/bottom_nav_back",还需要添加 paddingStartpaddingEnd

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:itemIconTint="@drawable/bottom_navigation_colors"
    app:itemTextColor="@drawable/bottom_navigation_colors"
    app:itemBackground="@drawable/bottom_nav_back"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:labelVisibilityMode="unlabeled"
    android:paddingStart="@dimen/_45sdp"
    android:paddingEnd="@dimen/_45sdp"
    android:paddingTop="@dimen/_8sdp"
    android:paddingBottom="@dimen/_5sdp"
    app:menu="@menu/bottom_nav_menu" />

结果如下图所示:

在BottomNavigationMenu中的圆角菜单项

英文:

Finally, I find a solution to my problem

  1. Add to the bg_prog_round.xml file

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >

     &lt;solid android:color=&quot;@color/edit_text_back&quot; /&gt;
    
     &lt;stroke
         android:width=&quot;0dp&quot;
         android:color=&quot;@color/edit_text_back&quot; /&gt;
    
     &lt;corners
         android:bottomLeftRadius=&quot;10dp&quot;
         android:bottomRightRadius=&quot;10dp&quot;
         android:topLeftRadius=&quot;10dp&quot;
         android:topRightRadius=&quot;10dp&quot; /&gt;
    

    </shape>

  2. Create another XML file and add this to it (bg_prog_orange_round.xml)

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

     &lt;solid android:color=&quot;@color/orange&quot; /&gt;
    
     &lt;stroke
         android:width=&quot;2dp&quot;
         android:color=&quot;@color/orange&quot; /&gt;
    
     &lt;corners
         android:bottomLeftRadius=&quot;@dimen/_15sdp&quot;
         android:bottomRightRadius=&quot;@dimen/_15sdp&quot;
         android:topLeftRadius=&quot;@dimen/_15sdp&quot;
         android:topRightRadius=&quot;@dimen/_15sdp&quot; /&gt;
    

    </shape>

  3. Create bottom_back_nav.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bg_prog_orange_round" android:state_checked="true" />
    <item android:drawable="@drawable/bg_prog_round" />
    </selector>

  4. Add this to your BottomNavigationView -> app:itemBackground=&quot;@drawable/bottom_nav_back&quot;

also, add paddingStart, paddingEnd to it

> <com.google.android.material.bottomnavigation.BottomNavigationView
> android:id="@+id/bottom_nav"
> android:layout_width="match_parent"
> android:layout_height="wrap_content"
> android:layout_gravity="bottom"
> app:itemIconTint="@drawable/bottom_navigation_colors"
> app:itemTextColor="@drawable/bottom_navigation_colors"
> app:itemBackground="@drawable/bottom_nav_back"
> app:layout_constraintBottom_toBottomOf="parent"
> app:layout_constraintEnd_toEndOf="parent"
> app:layout_constraintLeft_toLeftOf="parent"
> app:layout_constraintRight_toRightOf="parent"
> app:layout_constraintStart_toStartOf="parent"
> app:labelVisibilityMode="unlabeled"
> android:paddingStart="@dimen/_45sdp"
> android:paddingEnd="@dimen/_45sdp"
> android:paddingTop="@dimen/_8sdp"
> android:paddingBottom="@dimen/_5sdp"
> app:menu="@menu/bottom_nav_menu" />

And the result is here
在BottomNavigationMenu中的圆角菜单项

答案2

得分: 0

我建议您使用一个库来创建这种类型的菜单,我找到了这个 https://github.com/gauravk95/bubble-navigation,可能会有帮助。

英文:

I recommend you to use a library to do this kind of menu, I found this one https://github.com/gauravk95/bubble-navigation which can be helpful

答案3

得分: 0

你可以在你的布局文件中添加以下内容:

<RelativeLayout
    android:id="@+id/rel_titleText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="@dimen/_20dp"
    android:layout_marginTop="265dp"
    android:layout_marginRight="@dimen/_20dp"
    android:gravity="center|right">

    <!-- 其他内容 -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="3dp"
        android:background="@drawable/gradient_in_purchase"
        android:paddingTop="@dimen/_10dp"
        android:paddingBottom="@dimen/_10dp">

        <ImageView
            android:layout_marginRight="@dimen/_20dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_pro1"
            android:layout_width="@dimen/_24dp"
            android:layout_height="@dimen/_24dp"/>

    </RelativeLayout>
</RelativeLayout>

在你的drawable文件夹中添加名为 gradient_in_purchase 的XML文件,添加以下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <gradient
        android:startColor="@color/orange"
        android:angle="0"/>

    <corners
        android:bottomLeftRadius="@dimen/_10dp"
        android:bottomRightRadius="@dimen/_10dp"
        android:topLeftRadius="@dimen/_10dp"
        android:topRightRadius="@dimen/_10dp"/>

</shape>
英文:

You can Add in your layout file

        &lt;RelativeLayout
            android:id=&quot;@+id/rel_titleText&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_gravity=&quot;center&quot;
            android:layout_marginLeft=&quot;@dimen/_20dp&quot;
            android:layout_marginTop=&quot;265dp&quot;
            android:layout_marginRight=&quot;@dimen/_20dp&quot;
            android:gravity=&quot;center|right&quot;&gt;
            &lt;!----&gt;
            &lt;RelativeLayout
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:layout_gravity=&quot;bottom&quot;
                android:layout_margin=&quot;3dp&quot;
                android:background=&quot;@drawable/gradient_in_purchase&quot;
                android:paddingTop=&quot;@dimen/_10dp&quot;
                android:paddingBottom=&quot;@dimen/_10dp&quot;&gt;

             &lt;ImageView
                    android:layout_marginRight=&quot;@dimen/_20dp&quot;
                    android:layout_alignParentRight=&quot;true&quot;
                    android:src=&quot;@drawable/ic_pro1&quot;
                    android:layout_width=&quot;@dimen/_24dp&quot;
                    android:layout_height=&quot;@dimen/_24dp&quot;/&gt;

           &lt;/RelativeLayout&gt;
  &lt;/RelativeLayout&gt;

Add gradient_in_purchase XML in your drawable folder and add below code

     &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
     &lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
     android:shape=&quot;rectangle&quot;&gt;
     &lt;gradient
    android:startColor=&quot;@color/orange&quot;
    
    android:angle=&quot;0&quot;/&gt;
    &lt;corners
    android:bottomLeftRadius=&quot;@dimen/_10dp&quot;
    android:bottomRightRadius=&quot;@dimen/_10dp&quot;
    android:topLeftRadius=&quot;@dimen/_10dp&quot;
    android:topRightRadius=&quot;@dimen/_10dp&quot;/&gt;

    &lt;/shape&gt;

huangapple
  • 本文由 发表于 2020年9月30日 15:13:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64132611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定