I am using a TabLayout
and ViewPager
to display ActionBar tabs
following the guide Google Play Style Tabs using TabLayout, however my tabs are squished to the left side of the ActionBar, shown below:
And I would like them to take up the whole bar with equal widths. I've made only a few minor changes to the guide:
In activity_main.xml a style was created to show the ActionBar:
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/AppTheme"
app:tabMode="scrollable" />
Here is the styles.xml code:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">true</item>
<item name="tabIndicatorColor">#ffff0030</item>
</style>
Also, my MainActivity
now extends AppCompatActivity
instead of a FragmentActivity
.
You can refer to the TabLayout.
GRAVITY_CENTER
Gravity used to lay out the tabs in the center of the TabLayout.GRAVITY_FILL
Gravity used to fill the TabLayout as much as possible.MODE_FIXED
Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.MODE_SCROLLABLE
Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs.Set this in your code or your layout xml.
or
Generally, using the code like blow can work without setting
tabGravity
andtabMode
.