
Androidでタブを使う際に、タブ間のスペースを調整する方法についてのメモ。
Androidでデフォルトのタブウィジェットを使うとタブ間適度なスペースを設けてくれますが、カスタムタブを利用すると、スペースがなくなり、タブ同士がくっついてしまいます。
TabWidgetに対してDividerをセットすることで調節可能となります。
【構成】

【MainActivity.java】
package com.ayakix;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Dividerをセットする
tabHost.getTabWidget().setDividerDrawable(R.drawable.tabdivider);
// タブの初期化
tabHost.addTab(tabHost.newTabSpec("AA")
.setIndicator("AA")
.setContent(R.id.content));
tabHost.addTab(tabHost.newTabSpec("BB")
.setIndicator("BB")
.setContent(R.id.content));
tabHost.addTab(tabHost.newTabSpec("CC")
.setIndicator("CC")
.setContent(R.id.content));
}
}
【main.xml】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<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">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/content"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</FrameLayout>>
</TabHost>
</LinearLayout>
【tabdivider.xml】
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size
android:width="20px"
/>
<solid
android:color="#00000000"
/>
</shape>
※widthの値を変更することにより、タブ間のスペースを調整できます。また、colorを変更することにより、色を変更できます。