【问题标题】:TabLayout icons by using resource files使用资源文件的 TabLayout 图标
【发布时间】:2018-12-24 18:33:13
【问题描述】:

我想创建顶部对齐的标签。我想知道是否可以使用app:menu/top_home_menu 添加menu.xml 内的项目中列出的图标。如果没有,还有比TabLayout.getTabAt(index).setIcon(directory)更简单的方法吗?

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop = "true">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TabLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tabs"
                android:background="@drawable/white_grey_boarder_top"
                app:menu="@menu/top_home_menu">
            </android.support.design.widget.TabLayout>
        </android.support.design.widget.AppBarLayout>
    </RelativeLayout>
</merge>

我觉得就像bottomnavigationbar如何通过制作单独的菜单来添加图标

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary">

        <android.support.design.widget.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/bottomNavViewBar"
            android:background="@drawable/white_grey_boarder_top"
            app:menu="@menu/bottom_navigation_menu">

        </android.support.design.widget.BottomNavigationView>
    </RelativeLayout>
</merge>

【问题讨论】:

    标签: android icons android-tablayout


    【解决方案1】:

    在新版本的 TabLayout 中,谷歌添加了 TabItem,它可以通过您的 XML 轻松添加 Icon,代码如下:

    <android.support.design.widget.TabLayout
         app:tabTextColor="@color/gray"
         app:tabMode="fixed"
         app:tabBackground="@color/red"
         app:tabIndicatorHeight="4dp"
         app:tabIndicatorColor="@color/purple"
         app:tabPadding="2dp"
         app:tabSelectedTextColor="@color/white"
         app:tabMinWidth="64dp"
         android:layout_height="wrap_content"
         android:layout_width="match_parent">
    
     <!--add height and width to TabItem -->
     <android.support.design.widget.TabItem 
             android:text="@string/tab_text"/>
    
     <android.support.design.widget.TabItem
             android:icon="@drawable/ic1"/>
    

    更多信息请参考此链接link here

    希望这会有所帮助

    【讨论】:

    • 嗨,所以我想为包含图标链接的项目创建一个单独的菜单,并想知道我是否可以通过单独创建它来轻松地通过 xml 添加这些菜单
    • 或者你可以使用 tabLayout.getTabAt(position).setIcon(tabIcons[0]); 添加图标
    • 不是我想要的,但谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    相关资源
    最近更新 更多