gpt4 book ai didi

android - 带有自定义项的底部导航 View (actionLayout)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:05:14 24 4
gpt4 key购买 nike

我想在新的 BottomNavigationView 中添加自定义项目.

有很多关于使用普通导航 View 添加自定义 View 的教程,但我找不到关于底部的任何内容。

这是我的看法

   <android.support.design.widget.BottomNavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/menu_main" />

这是菜单

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

<item
android:id="@+id/menu_one"
android:icon="@drawable/ic_tab_one"
android:title="one" />
<item
android:id="@+id/menu_two"
app:actionLayout="@layout/item_action_notification"
android:title="two" />

如您所见,我像往常一样放置了 actionLayout 标签,但它根本没有显示。

有什么想法吗?谢谢。

最佳答案

希望以下解决方案对您有所帮助。

  1. 创建布局:layout/_custom_cart_item_layout.xml

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

    <TextView
    android:id="@+id/cart_badge"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_gravity="top|center_horizontal"
    android:layout_marginLeft="10dp"
    android:background="@drawable/circle"
    android:backgroundTint="@color/colorAccent"
    android:gravity="center"
    android:text="0"
    android:textColor="@android:color/white"
    android:textSize="12sp" />

    </FrameLayout>
  2. drawable/circle

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/white"/>
    <size android:width="40dp" android:height="40dp" />
    </shape>
  3. MainActivity : layout/main_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.MainActivity">

    <android.support.design.widget.BottomNavigationView
    android:id="@+id/bot_nav"
    android:layout_width="0dp"
    android:layout_height="56dp"
    android:background="@color/white"
    android:elevation="2dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:menu="@menu/bottom_menu" />

    </android.support.constraint.ConstraintLayout>
  4. 菜单:menu/bottom_menu.xml

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

    <item
    android:id="@+id/menu_home"
    android:icon="@drawable/home24"
    android:title="Home" />

    <item
    android:id="@+id/menu_cart"
    android:icon="@drawable/shoppingcart24"
    android:title="Cart" />
    <item
    android:id="@+id/menu_acct"
    android:icon="@drawable/user24"
    android:title="Account" />
    </menu>
  5. onCreate 内的 MainActivity 类中

    BottomNavigationView mbottomNavigationView =findViewById(R.id.bot_nav);

    BottomNavigationMenuView mbottomNavigationMenuView =
    (BottomNavigationMenuView) mbottomNavigationView.getChildAt(0);

    View view = mbottomNavigationMenuView.getChildAt(1);

    BottomNavigationItemView itemView = (BottomNavigationItemView) view;

    View cart_badge = LayoutInflater.from(this)
    .inflate(R.layout._custom_cart_item_layout,
    mbottomNavigationMenuView, false);

    itemView.addView(cart_badge);

Output : Image

希望它对您也有类似的作用。

谢谢

关于android - 带有自定义项的底部导航 View (actionLayout),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42653955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com