在底部导航视图中选定每个项目下方添加一行。

4

我希望在选择每个底部导航视图项目时,在其下方放置一条线。如下图所示,但我找不到方法来实现它。 我创建了一个形状线line.xml,如下所示:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke android:width="6dp" android:height="2dp" android:color="@color/honey"/>
    <corners android:radius="5dp"/>
</shape>

我创建了一个名为checked_uncheked.xml的选择器,代码如下:

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

我在bottomNavigation下面添加了"app:itemBackground="@drawable/line",但仍然没有结果。

enter image description here

1个回答

5
请使用这个line.xml文件:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="32dp"
        android:right="32dp"
        android:top="32dp">
        <shape android:shape="line">
            <stroke android:width="6dp" android:height="2dp" android:color="@color/honey"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</layer-list>

根据您的布局调整边距。


我尝试了你的解决方案,但仍然没有得到我想要的结果。 - Lina
1
@Lina 设置 app:itemBackground=@drawable/selector checked_uncheked - Saurabh Thorat
是的,谢谢你的帮助,但我仍然有一个问题。这一行显示在名称(主页、账户等)上方,我该如何将其放置在下方,并留出2dp的边距? - Lina
工作完成,谢谢。我只是改变了大小。 - Lina
嗨,Lina,我正在尝试实现相同的功能,但是我无法理解你所做的。你能否在这里发布更新后的答案? - Sid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接