TabHost.TabSpec
代码如下:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout android:id="@+id/settingTab" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/scheduleSetting" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/tv01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/setting_schedule" /> </LinearLayout> <LinearLayout android:id="@+id/taskSetting" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/tv02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24dip" android:text="@string/setting_task" /> </LinearLayout> </FrameLayout>
Activity:
public class SettingActivity extends TabActivity {
private TabHost tab = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tab = this.getTabHost();
LayoutInflater.from(this).inflate(R.layout.setting,
tab.getTabContentView(), true);
tab.addTab(tab.newTabSpec("schedulTab").setIndicator(
getString(R.string.setting_schedule),
this.getResources().getDrawable(R.drawable.calendar))
.setContent(R.id.scheduleSetting));
tab.addTab(tab.newTabSpec("taskTab").setIndicator(
getString(R.string.setting_task),
this.getResources().getDrawable(R.drawable.settings))
.setContent(R.id.taskSetting));
//下面设置icon和文字左右排;
TabWidget tabWidget = tab.getTabWidget();
for (int i = 0; i < tabWidget.getChildCount(); i++) {
TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(
android.R.id.title);
ImageView iv = (ImageView) tabWidget.getChildAt(i).findViewById(
android.R.id.icon);
iv.setPadding(10, 0, 0, 0);
tv.setPadding(50, 0, 0, 0);
RelativeLayout.LayoutParams paramsLeft = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsLeft.addRule(RelativeLayout.ALIGN_RIGHT);
paramsLeft.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
iv.setLayoutParams(paramsLeft);
RelativeLayout.LayoutParams paramsRight = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsRight.addRule(RelativeLayout.ALIGN_RIGHT);
paramsRight.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
tv.setLayoutParams(paramsRight);
tv.setTextColor(Color.BLACK);
}
}
}
效果如下图: