tabwidget を完全にカスタマイズする手順:
tabwidget のカスタム レイアウトを作成します。このレイアウトは、 oneIcon
と oneで構成されるデフォルトのレイアウトに似ていますTextView
。お好きなレイアウトをお選びいただけます。親レイアウトにはステートフルな background があることに注意してくださいtabwidget_selector
。このドローアブルは、デフォルトのオレンジ色のフォーカス カラーを置き換えるための鍵です。独自のフォーカス カラーを選択できます。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/tabwidget_selector"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/tabIndicatorIcon"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginTop="2dp" />
<TextView
android:id="@+id/tabIndicatorText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:textColor="@color/white" />
のビューを返す関数を記述しますtabwidget
。タブウィジェット用に描画可能なテキスト、アイコンを設定します
private View getTabIndicatorView(Context context, String tag, int drawable) {
View view = LayoutInflater.from(context).inflate(
R.layout.tab_widget_custom, null);
TextView tv = (TextView) view.findViewById(R.id.tabIndicatorText);
tv.setText(tag);
ImageView tabIndicatorIcon = (ImageView) view
.findViewById(R.id.tabIndicatorIcon);
tabIndicatorIcon.setBackgroundResource(drawable);
return view;
}
アクティビティでこの関数を使用します。
TabHost.TabSpec setIndicator(View view) ビューをタブ インジケータとして指定します。
Intent intent;
TabHost.TabSpec spec;
spec = tabHost.newTabSpec("Inbox").setIndicator(
getTabIndicatorView(MainTabActivity.this, "Hộp thư",
R.drawable.tin_nhan_tab_selector));
intent = new Intent(this, xxxx.InboxActivity.class);
spec.setContent(intent);
tabHost.addTab(spec);