红色的是res下drawable文件夹下的一个selector文件,内容是
[code="selector文件"]"@drawable/tab_timeline_normal" />
"@drawable/tab_timeline_active" />
"@drawable/tab_timeline_normal" />
"@drawable/tab_timeline_active" />
"@drawable/tab_timeline_normal" />
也是你按下时,和不按时等有一个效果上的改变,具体的可以参看关于selector的知识。绿色的就是两张不同效果的图片
[code="MainActivity"]public class MainActivity extends TabActivity {
private TabHost tabHost;
private RadioGroup mainbtGroup;
private static final String HOME = "主页";
private static final String REFER = "提及";
private static final String SECRET = "私信";
private static final String SEARCH = "搜索";
private static final String ATTENTIION = "关注";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhost);
tabHost = this.getTabHost();
View view1 = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView) view1.findViewById(R.id.tab_imageview_icon)).setImageResource(R.drawable.tab_timeline_selector);
((TextView) view1.findViewById(R.id.tab_textview_title)).setText(HOME);
TabHost.TabSpec spec1 = tabHost.newTabSpec(HOME)
.setIndicator(view1)
.setContent(new Intent(this, HomeTimeLineActivity.class));
tabHost.addTab(spec1);
View view2 = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView) view2.findViewById(R.id.tab_imageview_icon)).setImageResource(R.drawable.tab_atme_selector);
((TextView) view2.findViewById(R.id.tab_textview_title)).setText(REFER);
TabHost.TabSpec spec2 = tabHost.newTabSpec(REFER)
.setIndicator(view2)
.setContent(new Intent(this, ReferActivity.class));
tabHost.addTab(spec2);
View view3 = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView) view3.findViewById(R.id.tab_imageview_icon)).setImageResource(R.drawable.tab_message_selector);
((TextView) view3.findViewById(R.id.tab_textview_title)).setText(SECRET);
TabHost.TabSpec spec3 = tabHost.newTabSpec(SECRET)
.setIndicator(view3)
.setContent(new Intent(this, MessageActivity.class));
tabHost.addTab(spec3);
View view4 = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView) view4.findViewById(R.id.tab_imageview_icon)).setImageResource(R.drawable.tab_explore_selector);
((TextView) view4.findViewById(R.id.tab_textview_title)).setText(SEARCH);
TabHost.TabSpec spec4 = tabHost.newTabSpec(SEARCH)
.setIndicator(view4)
.setContent(new Intent(this, SearchActivity.class));
tabHost.addTab(spec4);
View view5 = View.inflate(MainActivity.this, R.layout.tab, null);
((ImageView) view5.findViewById(R.id.tab_imageview_icon)).setImageResource(R.drawable.tab_focus_selector);
((TextView) view5.findViewById(R.id.tab_textview_title)).setText(ATTENTIION);
TabHost.TabSpec spec5 = tabHost.newTabSpec(ATTENTIION)
.setIndicator(view5)
.setContent(new Intent(this, AttentionActivity.class));
tabHost.addTab(spec5);
}
关键是tabhost,tabcontent和tabs这三个id一定要正确红色的是tab的背景
是一张.9.png格式的图片,这个很有用哟在android里,经常用来处理图片拉升的问题。左边和上面的小点表示要拉伸的地方,右边和下面的表示内容区。关于.9.png格式图片在android里面得更多应用看这里
[code="tabhost布局文件"]android:background="@drawable/tab_bkg" android:fadingEdge="none"
android:fadingEdgeLength="0.0px" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
[code="tabhost布局文件"]android:layout_alignParentBottom="true" 把tab的五个按钮挨着父控件的底部,在android里面RelativeLayout很好用
[code="tab布局文件,就是你看到的那5个按钮的布局文件"]