添付のスクリーン ショットに示すようにタブ ホストを作成しようとしていますが、アイコン イメージとテキストの配置に問題があります。イメージをテキストの上に配置する必要がありますが、すべての試行が失敗します。
サンプルコードをいくつか含めましたが、テキストの画像の上揃えを強制することはできません.tab_indicatorファイルのlayout_heightを66dipより大きくした場合にのみ機能しますが、提供されるスペースと画像が制限されています. 画像/リンクが表示されない場合は、画像がテキストの上に強制されていないため、画像の一部がテキストの後ろにあります!
tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="88.0dip"
android:layout_height="66.0dip"
android:layout_weight="1.0">
<include layout="@layout/tab_indicator_portrait" />
</FrameLayout>
tabindicator_protrait.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_indicator_portrait"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="3.0dip"
android:paddingTop="3.0dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:layout_marginTop="3.0dip" />
<TextView
android:id="@+id/title"
style="?android:attr/tabWidgetStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:textColor="@color/tab_indicator_text"
android:textSize="12.0dip" />
</FrameLayout>
tab_host.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:id="@+id/topLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="0.0dip" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dip"
android:padding="0dip" />
</LinearLayout>
</TabHost>
このように設定した理由はいくつかあります。
- tab_indicator.xml ファイルには、ランドスケープを別の方法で処理する必要がある場合、つまり、市場のフィードバック/テストに基づいてさらにリリースする必要がある場合に、ランドスケープ ファイルを含めることもできます。
- ユーザーがタブ間を移動したときにのみアイコンを変更したいので、アプリケーションに一般的なルックアンドフィールを持たせることができます - 4.0.4 でタブホストに問題があり、これはそれを処理するよりクリーンな方法のようです。
画面サイズに基づいてレイアウト サイズを処理するために、TabActivity でいくつかのコードを試しました。
private void detectScreen()
{
Display localDisplay = getWindowManager().getDefaultDisplay();
if (localDisplay.getWidth() < localDisplay.getHeight())
{
int dips = 45;
TabHost localTabHost = getTabHost();
DisplayMetrics localDisplayMetrics = getResources().getDisplayMetrics();
for (int i = 0; i < localTabHost.getTabWidget().getTabCount(); i++)
{
View localView = localTabHost.getTabWidget().getChildTabViewAt(i);
localView.getLayoutParams().height = ImageUtils.convertDIPToPixels(localDisplayMetrics, (float)dips);
}
}
}
ただし、レイアウトのサイズを変更すると、画像は小さくなりますが、テキストとの位置合わせはできません。
マニフェスト ファイルには、次の内容も含まれています。
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true"/>