如何在Tabhost中更改Tabs的测试大小?

问题描述 投票:0回答:1

如何更改选项卡标签的文本大小。我知道有很多类似的问题,但我对android平台很新,不使用android.support.design.widget.TabLayout。 stackoverflow或其他站点中有关此问题的每个示例都包含TableLayout。如何用Tabhost解决这个问题?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.mobiders.activity.MainActivity"
tools:showIn="@layout/app_bar_main">

<TabHost
    android:id="@+id/tab_host"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true">

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

        <HorizontalScrollView
            android:id="@+id/hscrollview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </HorizontalScrollView>

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

            </LinearLayout>

            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></ListView>

        </FrameLayout>
    </LinearLayout>
</TabHost>

<FrameLayout
    android:id="@+id/fl_interceptor"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants" />

<include
    layout="@layout/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="0dp" />

</RelativeLayout>
android android-tabhost text-size
1个回答
1
投票

您可以以编程方式执行此操作。在您使用tabhost的活动中,请使用以下代码

     TabHost tabhost = getTabHost();
            for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) 
            {
                TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
                int textSize = 14;
                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize );
            }

然后,在更改选项卡上,您可以使用以下代码更改文本大小:

        tabHost.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {

                for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {

                    TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
                    int textSize = 14;
                    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize );
                }


                TextView tv = (TextView) tabhost.getCurrentTabView().findViewById(android.R.id.title); //for Selected Tab
                    int textSize2 = 14;
                    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP,textSize2);


            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.