我想创建顶部对齐的标签。我想知道是否可以通过使用menu.xml
添加app:menu/top_home_menu
中项目中列出的图标。如果不是,除了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>
答案 0 :(得分:0)
在新版的TabLayout中,谷歌添加了TabItem,可以轻松地通过XML使用以下代码添加图标:
<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
希望有帮助