【问题标题】:How to make the text center in TabPageIndicator如何在 TabPageIndicator 中使文本居中
【发布时间】:2016-09-06 15:51:58
【问题描述】:

嗨,伙计们,我在我的应用程序中使用了 TabPageIndicator,一切正常。嗯,但是 PageTitle 不能像这样在中心:

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

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

但它不起作用。 那么如何使它工作呢?感谢您的查看和帮助。

【问题讨论】:

    标签: android android-layout styles


    【解决方案1】:

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

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

    【讨论】:

    • 嗯,我必须使用 TabPageIndicator + ViewPage.so 不行。但是还是谢谢你。
    猜你喜欢
    • 2016-09-30
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 2014-07-21
    • 2011-11-19
    • 2021-01-10
    • 2014-07-09
    相关资源
    最近更新 更多