2016-05-11 23 views
0

嗨,大家好,我在我的应用程序中使用TabPageIndicator,一切都是ok.eh,但的PageTitle不能在这样的中心: enter image description here如何使文本中心TabPageIndicator

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

<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> 

,但它不工作。 那么如何使它工作?感谢您的观点和帮助。

回答

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; 
     } 
    } 
} 
+0

嗯,我必须使用TabPageIndicator + ViewPage.so它不是OK.But感谢所有一样。 – buruoyanyang