Android 如何使tablayout文本大小相等?

Android 如何使tablayout文本大小相等?,android,tabs,Android,Tabs,下面是我所做的:我为文本创建了一个样式 <!-- Change tab text appearance --> <style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab"> <item name="textAllCaps">false</item> <item name="android:textAppearance"&

下面是我所做的:我为文本创建了一个样式

<!-- Change tab text appearance -->
    <style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
        <item name="textAllCaps">false</item>
        <item name="android:textAppearance">@style/CustomTabWidgetText</item>
    </style>

    <style name="CustomTabWidgetText"
        parent="@android:style/TextAppearance.Widget.TabWidget">
        <item name="android:textSize">16sp</item>
    </style>

假的
@样式/自定义TabWidgetText
16便士
然后我把它放在我的表格上

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".activity.HomeActivity"
    tools:showIn="@layout/app_bar_main">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:titleTextColor="#ffffff"
        />


    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        app:tabSelectedTextColor="#ffffff"
        app:tabTextAppearance="@style/MyCustomTextAppearance"
        app:tabTextColor="#ffffff" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tab_layout" />

</RelativeLayout>


如您所见,“D日完成”文本比其他文本小。我要求把它的尺寸和别人的一样大,但我不知道怎么做。请帮帮我,谢谢

您可以尝试在TabLayout中设置填充 (app:tabPaddingStart=“-1dp”,app:tabPaddingEnd=“-1dp”)



它帮助了我)

在经历了类似的事情之后,在阅读了TabLayout源代码之后,我尝试覆盖dimens.xml文件中的维度,如下所示:

<dimen name="design_tab_text_size_2line" tools:override="true">48sp</dimen>
<resources xmlns:tools="http://schemas.android.com/tools">
    tabLayout = findViewById(R.id.tabs);
    ((TextView) tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tabTextView)).setText(R.string.tab1);
    ((TextView) tabLayout.getTabAt(1).getCustomView().findViewById(R.id.tabTextView)).setText(R.string.tab2);
    ((TextView) tabLayout.getTabAt(2).getCustomView().findViewById(R.id.tabTextView)).setText(R.string.tab3);
    ((TextView) tabLayout.getTabAt(3).getCustomView().findViewById(R.id.tabTextView)).setText(R.string.tab4);
48sp
不要忘记在文件的根目录中添加名称空间,如下所示:

<dimen name="design_tab_text_size_2line" tools:override="true">48sp</dimen>
<resources xmlns:tools="http://schemas.android.com/tools">
    tabLayout = findViewById(R.id.tabs);
    ((TextView) tabLayout.getTabAt(0).getCustomView().findViewById(R.id.tabTextView)).setText(R.string.tab1);
    ((TextView) tabLayout.getTabAt(1).getCustomView().findViewById(R.id.tabTextView)).setText(R.string.tab2);
    ((TextView) tabLayout.getTabAt(2).getCustomView().findViewById(R.id.tabTextView)).setText(R.string.tab3);
    ((TextView) tabLayout.getTabAt(3).getCustomView().findViewById(R.id.tabTextView)).setText(R.string.tab4);

这对我很有用

希望有帮助

编辑:

它似乎并不适用于所有情况(实际上,当文本有两行或更多行时,它应该起作用),但当样式不起作用时,它会起作用。所以我要做的是,我使用了两种技术(样式和覆盖维度)。

根据,这对我来说非常有效:

    <android.support.design.widget.TabLayout
       android:id="@+id/tab_layout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:tabMode="scrollable"
       app:tabGravity="fill"
       app:tabIndicatorHeight="5dp"
    />

这对我来说很有用:

app:tabMode="scrollable"
app:tabGravity="fill"

在我看来,最好的方法是使用自定义视图。你将拥有最大的灵活性。例如:

<com.google.android.material.tabs.TabLayout
       android:id="@+id/tabs"
       android:layout_width="match_parent"
       android:layout_height="36dp"
       android:layout_marginLeft="16dp"
       android:layout_marginRight="16dp"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       app:tabGravity="fill"
       app:tabMode="fixed">

       <com.google.android.material.tabs.TabItem
           android:id="@+id/tab1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout="@layout/view_tab_text_layout" />

       <com.google.android.material.tabs.TabItem
           android:id="@+id/tab2"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout="@layout/view_tab_text_layout" />

       <com.google.android.material.tabs.TabItem
           android:id="@+id/tab3"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout="@layout/view_tab_text_layout" />

       <com.google.android.material.tabs.TabItem
           android:id="@+id/tab4"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout="@layout/view_tab_text_layout" />

   </com.google.android.material.tabs.TabLayout>

完整的工作示例可以在这里找到:

您肯定可以在这里找到您的答案……如果文本较大,则不适合。您将tabLayout的tabMode设置为
app:tabMode=“scrollable”
,这会解决问题吗?然后,也许整个文本都适合你,你会有相同的文本大小。这在你的XML中,在 TabPays TAG中。或者如果你有一个已知的标签数量,考虑保持TabMoad固定并缩小文本宽度在小宽度的屏幕上,这是唯一的答案,这个技巧…谢谢你@ Serg0!我不知道它为什么有效,但它确实有效!非常感谢@Feliperibeirror.Magalhaes文本变小的原因是它对于选项卡来说太宽了,所以字体大小会自动减小,以便适合。通过减小选项卡的水平填充,文本有更多的空间,因此可以适合正常的字体大小。但这完全取决于文本的长度。尽管有额外的空间,但仍然不适合的文本仍将变小。@Serg0请您确切解释一下为什么
-1
?不是
0
。它对我来说是双向的,但无论如何,base
View
类使用
-1
作为defParam。有什么系统标志吗?@Merov我只是假设无效参数会禁用该“功能”,并且它起作用。我不确定0是否在这种情况下工作——2.5年过去了,Android两个支持库都发生了巨大的变化。奇怪的是,没有人对这个答案投赞成票。在我的例子中,只有当我覆盖了design_tab_text_size_2line维度时,这才起作用。无论如何,谢谢,@thenaoh。请提供一些关于上述代码如何回答问题的信息,以改进此答案。