问题
Here is what I did: I created a style for the text
<!-- 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>
then I set it to my tablayout
<?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>
Here is the result:
As you can see, the "D-day complete" text is smaller than others. I have request to make its size equal to others but I dont know how. Please help me, thanks.
回答1:
You can try to set padding in TabLayout (app:tabPaddingStart="-1dp", app:tabPaddingEnd="-1dp")
like
<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"
app:tabPaddingStart="-1dp"
app:tabPaddingEnd="-1dp"/>
It helped me)
回答2:
Per this post, this worked really well for me:
<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"
/>
The tabMode
and tabGravity
attributes did the trick. This lets the labels span as long as need be and scroll like so:
回答3:
After experiencing something similar, and after reading the TabLayout source code, I try overriding a dimension, in my dimens.xml file, like this:
<dimen name="design_tab_text_size_2line" tools:override="true">48sp</dimen>
and don't forget to add the namespace in the root of your file, like this:
<resources xmlns:tools="http://schemas.android.com/tools">
and it works for me.
Hope it helps!
EDIT :
It seems like it doesn't work on every situation (it's actually supposed to work when your text has two lines or more), but it helps when the style doesn't work. So what I do is that I use both technics (style and overridden dimension).
回答4:
This is works for me:
app:tabMode="scrollable"
app:tabGravity="fill"
来源:https://stackoverflow.com/questions/36399013/how-to-make-tablayout-text-size-equal