TabHost.TabSpec中的标签实现图文左右排(默认上下排)

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);
		}
	}
}

 

效果如下图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值