【问题标题】:Aligning fonts in tabwidget在 tabwidget 中对齐字体
【发布时间】:2026-01-13 07:35:01
【问题描述】:

我自己设计了一个使用 GPS 和地图的应用程序。我已将 UI 创建为选项卡。但我无法将字体保留在图像下方。

我的 Mainactivity 中的代码

TabSpec gpsspec = tabHost.newTabSpec("GPS");
gpsspec.setIndicator("GPS", getResources().getDrawable(R.drawable.gps));
Intent gpsIntent = new Intent(this, GpsActivity.class);
gpsspec.setContent(gpsIntent);
tabHost.addTab(gpsspec);

标签的可绘制文件夹中的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/gps_icon"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/gps_icon" />
</selector>

Main.xml

<?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="fill_parent"
    android:layout_height="fill_parent" >

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

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

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

</TabHost>

请帮助对齐字体。

【问题讨论】:

    标签: android-tabhost tabwidget android-tabactivity android-fonts


    【解决方案1】:

    您需要提供

    • drawable-ldpi 文件夹中用于小密度设备的小图标
    • drawable-mdpi 文件夹中中等密度设备的中等大小图标
    • drawable-hdpi 文件夹中用于大密度设备的大尺寸图标

    Here 是可绘制和布局大小的文档指南。

    如果它不能解决您的问题,您可以考虑提供您自己的自定义标签指示器,我在my post here 中使用过。

    【讨论】: