如何在TabPageIndicator中创建文本中心

时间:2016-05-11 10:06:47

标签: android android-layout styles

大家好,我在我的应用程序中使用TabPageIndicator,一切都很好。但是PageTitle不能像这样在中心: enter image description here

你看,它在左边。 我尝试使用自定义样式:

    <style  name="LightTabPageIndicator"  parent="Material.Widget.TabPageIndicator">
        <item name="tpi_tabPadding">12dp</item>
        <item name="android:gravity">center</item>
        <item name="tpi_tabRipple">@style/LightTabRippleStyle</item>
        <item name="tpi_indicatorHeight">3dp</item>
        <item name="tpi_indicatorColor">@color/md_blue_400</item>
        <item name="android:textAppearance">@style/LightTabTextAppearance</item>
        <item name="android:background">@color/md_grey_200</item>
        <item name="tpi_mode">fixed</item>
    </style>
    <style name="LightTabRippleStyle" parent="Material.Drawable.Ripple.Wave">
        <item name="android:background">@null</item>
        <item name="rd_rippleColor">@color/md_grey_500</item>
        <item name="rd_rippleAnimDuration">300</item>
        <item name="rd_maskType">rectangle</item>
        <item name="rd_cornerRadius">0dp</item>
        <item name="rd_padding">0dp</item>
    </style>
    <style name="LightTabTextAppearance" parent="@style/Base.TextAppearance.AppCompat.Subhead">
        <item name="android:textColor">@drawable/color_tpi_dark</item>
        <item name="android:layout_gravity">center</item>
        <item name="android:gravity">center</item>
    </style>

但它不起作用。 那么如何让它发挥作用?谢谢你的观点和帮助。

1 个答案:

答案 0 :(得分:0)

试试这个: 为每个添加的选项卡使用android.widget.TabHost.TabSpec #setIndicator(CharSequence标签)

int tabCount = tabHost.getTabWidget().getTabCount();
for (int i = 0; i < tabCount; i++) {
    final View view = tabHost.getTabWidget().getChildTabViewAt(i);
    if ( view != null ) {
        // reduce height of the tab
        view.getLayoutParams().height *= 0.66;

        //  get title text view
        final View textView = view.findViewById(android.R.id.title);
        if ( textView instanceof TextView ) {
            // just in case check the type

            // center text
            ((TextView) textView).setGravity(Gravity.CENTER);
            // wrap text
            ((TextView) textView).setSingleLine(false);

            // explicitly set layout parameters
            textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT;
            textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
        }
    }
}