Android:如何在自定义的tabLayout中使用滚动填充选项卡?

3

我有一个包含6个选项卡的tablayout。这些选项卡在显示时是固定的,但它们的文本内容没有完全显示出来。我在这里找到了解决方案,以下是总结:

<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,但我仍然遇到同样的问题。
以下是我的代码:
我使用了一个名为custom_tab.xml的文件来自定义tablayout
   <?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 显示标签图标,下一个 ImageView 用于徽章,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宽度,使其达到最大存在的制表符大小?或者有其他解决方案吗?

你想让你的选项卡布局可以滚动还是不可滚动? - Red M
我想要可以滚动的。 - BeginnerProgrammer
1个回答

0

尝试消除

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

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

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


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