首页 > 代码库 > Android开发之自定义TabHost文字及背景(源代码分享)
Android开发之自定义TabHost文字及背景(源代码分享)
使用TabHost 可以在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该如何进行自定义修改优化呢
MainActivity的源代码
package com.dream.ledong; import android.app.TabActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Gravity; import android.widget.RelativeLayout; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; import android.widget.TabWidget; import android.widget.TextView; import com.example.client.R; @SuppressWarnings("deprecation") public class itemList extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.itemlist); final TabHost tabHost = getTabHost(); Intent remoteIntent = new Intent(itemList.this, item1.class); TabHost.TabSpec remoteTabSpec = tabHost.newTabSpec("remote"); remoteTabSpec.setIndicator("运动推荐"); remoteTabSpec.setContent(remoteIntent); tabHost.addTab(remoteTabSpec); Intent localIntent = new Intent(itemList.this, item2.class); TabHost.TabSpec localTabSpec = tabHost.newTabSpec("local"); localTabSpec.setIndicator("球友人气"); localTabSpec.setContent(localIntent); tabHost.addTab(localTabSpec); Intent localIntent2 = new Intent(itemList.this, item2.class); TabHost.TabSpec localTabSpec2 = tabHost.newTabSpec("a"); localTabSpec2.setIndicator("竞技氛围"); localTabSpec2.setContent(localIntent2); tabHost.addTab(localTabSpec2); updateTabStyle(tabHost); // 当某个Tab被选中时,则更新背景样式 tabHost.setOnTabChangedListener(new OnTabChangeListener() { @Override public void onTabChanged(String tabId) { updateTabStyle(tabHost); } }); } private void updateTabStyle(final TabHost mTabHost) { TabWidget tabWidget = mTabHost.getTabWidget(); tabWidget.setRightStripDrawable(R.drawable.list_item_divide_operate); tabWidget.setLeftStripDrawable(R.drawable.list_item_divide_operate); for (int i = 0; i < tabWidget.getChildCount(); i++) { RelativeLayout tabView = (RelativeLayout) mTabHost.getTabWidget() .getChildAt(i); TextView text = (TextView) tabWidget.getChildAt(i).findViewById( android.R.id.title); text.setTextSize(15); RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text .getLayoutParams(); params.width = RelativeLayout.LayoutParams.MATCH_PARENT; params.height = RelativeLayout.LayoutParams.MATCH_PARENT; text.setLayoutParams(params); text.setGravity(Gravity.CENTER); if (mTabHost.getCurrentTab() == i) { // 选中 tabView.setBackgroundColor(Color.parseColor("#8DB6CD")); text.setTextColor(this.getResources().getColorStateList( android.R.color.black)); } else { // 未选中 tabView.setBackgroundColor(Color.parseColor("#ffffff")); text.setTextColor(this.getResources().getColorStateList( android.R.color.darker_gray)); } } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。