【问题标题】:How to change the tabwidget color and tab text size in android studio?如何在 android studio 中更改 tabwidget 颜色和 tab 文本大小?
【发布时间】:2016-09-01 04:33:04
【问题描述】:

大家好,我正在开发一个 android 项目。我想更改选项卡小部件的条带颜色并更改选项卡文本的大小。请告诉我解决方案我该怎么做?

这是代码:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/holo_blue_dark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="30dp"
            android:elevation="0dp"
            android:showDividers="none"
            android:tabStripEnabled="false" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</TabHost>

【问题讨论】:

标签: android performance android-layout android-fragments android-studio


【解决方案1】:

改变文字大小

它不漂亮,但试试这个 Dirty Fix :

TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);
View tabView = tw.getChildTabViewAt(0);
TextView tv = (TextView)tabView.findViewById(android.R.id.title);
tv.setTextSize(20);

 //Do this to hack font size of title text
 LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
 TabWidget tw = (TabWidget) ll.getChildAt(0);

 // for changing the text size of first tab
 RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
 TextView lf = (TextView) rllf.getChildAt(1);
 lf.setTextSize(21);
 lf.setPadding(0, 0, 0, 6);

final TabWidget tw = (TabWidget)mTabHost.findViewById(android.R.id.tabs);
for (int i = 0; i < tw.getChildCount(); ++i)
{
   final View tabView = tw.getChildTabViewAt(i);
   final TextView tv = (TextView)tabView.findViewById(android.R.id.title);
   tv.setTextSize(20);
}

【讨论】:

  • 但在我的代码中没有文本视图,那么我如何在 java 文件中获取文本视图?
【解决方案2】:
<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>
<com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/welcome_menu_icon_height_10"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/margin_15"
        android:background="@android:color/transparent"
        android:nestedScrollingEnabled="true"
        android:scrollbarThumbVertical="@color/white"
        android:textAlignment="center"
        app:tabGravity="center"
        app:tabIndicatorAnimationDuration="1"
        app:tabIndicatorColor="@color/white"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="@color/white"
        app:tabTextAppearance="@style/MyTabTextAppearance"
        app:tabTextColor="@color/white" />

 tabLayout.addTab(tabLayout.newTab().setText(R.string.casa))
        tabLayout.addTab(tabLayout.newTab().setText(R.string.fdr_dps))
        tabLayout.addTab(tabLayout.newTab().setText(R.string.loan))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    相关资源
    最近更新 更多