android tabs

tamaño de fuente de Android de pestañas



tabs (7)

Escriba estos códigos a continuación en styles.xml

<style name="MyTabLayout" parent="Base.Widget.Design.TabLayout"> <item name="tabTextAppearance">@style/MyTabTextAppearance</item> </style> <style name="MyTabTextAppearance" parent="TextAppearance.AppCompat.Button"> <item name="android:textSize">18sp</item> <item name="android:textColor">@android:color/white</item> <item name="textAllCaps">true</item> </style>

Y en tu tablayout, establece el estilo como se muestra a continuación.

<android.support.design.widget.TabLayout style="@style/MyTabLayout" android:layout_width="width" android:layout_height="height"/>

Hago esta búsqueda hace un tiempo, pero no obtengo soluciones :( mi problema es que tengo una aplicación de Android con una actividad de pestañas, donde tengo que configurar el tamaño de fuente de mis pestañas, pero no sé cómo.

en mi actividad configuré mis pestañas mediante programación:

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); tabLayout.addTab(tabLayout.newTab().setText("MY TAB 1")); tabLayout.addTab(tabLayout.newTab().setText("MY TAB 2")); tabLayout.addTab(tabLayout.newTab().setText("MY TAB 3")); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

El problema es que las últimas 1 - 2 letras se cortarán, así que tengo que configurar el tamaño de fuente más pequeño, pero ¿cómo?

Espero que alguien pueda ayudarme.


He hecho lo siguiente:

Cree una entrada attrs.xml como esta

<resources> <declare-styleable name="SegmentedTabLayout"> <attr name="fontSize" format="dimension" /> </declare-styleable> </resources>

Luego cree una propiedad de tamaño de fuente e inicie el método que llamará en el constructor como este

public float FontSize { get; set; } private void InitializeAttrs(Context context, IAttributeSet attrs) { if (context == null || attrs == null) return; var styleAttributes = context.ObtainStyledAttributes(attrs, Resource.Styleable.SegmentedTabLayout); if (styleAttributes == null) return; FontSize = styleAttributes.GetDimension(Resource.Styleable.SegmentedTabLayout_fontSize, -1); styleAttributes.Recycle(); }

Luego usé el método viral 9966 (lo mejoré un poco) y lo llamé en el método de anulación NewTab como este

public override Tab NewTab() { var tab = base.NewTab(); ChangeFontSize(tab); return tab; } private void ChangeFontSize(Tab tab) { if (FontSize < 0) return; var tabViewGroup = (ViewGroup)tab.View; for (int i = 0; i < tabViewGroup.ChildCount; i++) { if (tabViewGroup.GetChildAt(i) is TextView textView) { textView.TextSize = FontSize; } } }

Y eso es :)


Si desea cambiar el tamaño de fuente mediante programación, puede usar la reflexión de Java para acceder al campo entero tabTextSize en la clase TabLayout y establecer el tamaño de fuente según sus requisitos.

#If you dont want to use style.xml and do by prgramatically the Use this in your xml layout. # <android.support.design.widget.TabLayout android:id="@+id/tab_Layout_dashboard" android:layout_below="@id/ll_profile" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.15" style="@style/Base.Widget.Design.TabLayout" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> <android.support.v4.view.ViewPager android:layout_below="@id/tab_Layout_dashboard" android:id="@+id/pager_tutor" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.85" /> #In your activity or fragment use this method# private void changeTabsFont() { Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/"+ Constants.FontStyle); ViewGroup vg = (ViewGroup) tab_layout.getChildAt(0); int tabsCount = vg.getChildCount(); for (int j = 0; j < tabsCount; j++) { ViewGroup vgTab = (ViewGroup) vg.getChildAt(j); int tabChildsCount = vgTab.getChildCount(); for (int i = 0; i < tabChildsCount; i++) { View tabViewChild = vgTab.getChildAt(i); if (tabViewChild instanceof TextView) { ((TextView) tabViewChild).setTypeface(font); ((TextView) tabViewChild).setTextSize(15); } } } }


intenta también:

<style name="MyCustomTabText" parent="TextAppearance.Design.Tab"> <item name="android:textSize">14sp</item> <item name="android:textColor">?android:textColorSecondary</item> <item name="textAllCaps">false</item> </style> <android.support.design.widget.TabLayout android:id="@+id/tabs" app:tabTextAppearance="@style/MyCustomTabText" android:layout_width="match_parent" android:layout_height="wrap_content" />


prueba esto

Cree un diseño xml llamado custom_tab.xml

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tab" android:textColor="@color/colorAccent"/>

que en su actividad establezca el tamaño del texto programáticamente como el siguiente código

TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); tabOne.setText("ONE"); tabOne.setTextSize(14); // set font size as per your requirement tabLayout.getTabAt(0).setCustomView(tabOne);


si configura text_size directamente en el estilo de diseño de pestaña, ¡no funciona! debes configurarlo en un estilo que tenga parent = "@ android: style / TextAppearance.Widget.TabWidget"

public static void updateTabLayoutFontSize(TabLayout tabLayout, int textSizeInPixel) { try { Field mCursorDrawableRes = TabLayout.class.getDeclaredField("tabTextSize"); mCursorDrawableRes.setAccessible(true); mCursorDrawableRes.set(tabLayout, textSizeInPixel); } catch (Exception e) { Log.d("TAG1", "Failed to update tablayout font using reflection"); } }

y también este es un estilo completo para la pestaña:

<style name="tab_text" parent="@android:style/TextAppearance.Widget.TabWidget"> <item name="android:fontFamily">@fonts/iransans_b</item> <item name="android:textSize">@dimen/textSize_Large</item> <item name="textAllCaps">false</item> </style> <style name="Tab" parent="Widget.Design.TabLayout"> <item name="tabTextAppearance">@style/tab_text</item> </style> <com.google.android.material.tabs.TabLayout android:id="@+id/item_tabs" android:layout_width="match_parent" android:layout_height="@dimen/margin_dp_65" style="@style/Tab"/>


<style name="Tab_WithBackground" parent="Widget.Design.TabLayout"> <item name="tabSelectedTextColor">@color/purple</item> <item name="tabTextColor">@drawable/tab_text_color</item> <item name="tabIndicatorColor">@color/white</item> <item name="tabGravity">fill</item> <item name="tabIndicatorHeight">4dp</item> <item name="android:tabStripEnabled">true</item> <item name="android:padding">0dp</item> <item name="tabMaxWidth">0dp</item> <item name="android:minHeight">@dimen/margin_dp_80</item> <item name="tabTextAppearance">@style/tab_text</item> </style>

Este código funciona para que Tablayout cambie el color del texto, escriba la cara (estilo de fuente) y también el tamaño del texto. Si esto funciona para usted, entonces haga lo correcto para que otro usuario pueda resolver fácilmente su problema