首页 > 解决方案 > Android:如何在自定义 tabLayout 中通过滚动填充选项卡?

问题描述

我有tablayout6 个标签。选项卡在显示中是固定的,但它们的文本没有完全显示。我在这里做了解决方案,以便我可以在这些行中总结它们:

<android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMaxWidth="0dp"
        app:tabGravity="fill"
        app:tabMode="fixed" />

但这并没有解决我的问题。我也在这里使用自定义TabLayout我仍然有同样的问题。

这是我的代码:我对这个文件
使用自定义:tablayoutcustom_tab.xml

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:layout_gravity="center"
            android:layout_marginTop="8dp"
            android:layout_centerHorizontal="true"
            android:id="@+id/tabIcon"/>

        <CustomViews.CustomTextView
            android:id="@+id/basket_badge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="15dp"
            android:background="@drawable/notification_circle"
            android:padding="2dp"
            android:textColor="#ffffff"
            android:textSize="8sp" />
        <CustomViews.CustomTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/tab_inactive"
            android:textSize="8sp"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/tabIcon"
            android:layout_marginBottom="2dp"
            android:maxLines="1"
            android:id="@+id/tabTitle"/>

    </RelativeLayout>

第一个ImageView显示选项卡图标。nextImageView用于徽章,CustomTextView用于显示选项卡标题。
这是我的布局文件包括tablayout

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

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="scrollable"
        app:tabMaxWidth="0dp"
        android:layoutDirection="rtl"
        android:id="@+id/avatar_tabLayout"
        app:tabIndicatorColor="@null"
        />    
</LinearLayout>

有没有办法用最大存在标签大小设置tab宽度?还是有其他解决方案?

标签: androidandroid-tablayoutandroid-scrollable-tabs

解决方案


试着摆脱

app:tabGravity="fill"
app:tabMode="fixed"

在你的<android.support.design.widget.TabLayout

将其替换为app:tabMode="scrollable"


推荐阅读