1 先看看SDK中怎么说的
StateListDrawable
对象,就是根据对象的不同的状态用不同的image来表现相同的grapics,比如Button。只能有一个<selector>元素,可以有好几个<item>,用不同的属性来显示不同的状态。文件放在res/drawable/filename.xml中,语法如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=[ " true " false " ]
android:dither=[ " true " false "]
android:variablePadding=[ " true " false "]>
<item android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=[ " true " false "]
android:state_focused=[ "true " false "]
android:state_selected=[ " true " false "]
android:state_checkable=[ " true " false "]
android:state_checked=[ "true " false "]
android:state_enabled=[ " true " false "]
android:state_window_focused=[ " true " false "] />
</selector>
属性的解释:
<selector>
必须是根元素。
<item>
通过描述其属性来定义一个特定状态下的drawable。必须是<selector>
的子元素。
android:state_pressed:是否被按下,如按钮被单击;
android:state_focused:是否有焦点;
android:state_selected:是否被选中,如一个tab是打开的;
android:state_checkable:只用于能被checkable的对象上;同理android:state_checked;
android:state_enabled:对象能够接受如触摸,或单击;
android:state_window_focused:应用是否在前台。
Android会从上到下开始匹配,匹配到第一个合适的就停。
2 将Tab按钮和listitem的背景用<selector>
一个tab的selector定义如下:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false" android:drawable="@drawable/home_normal" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/home_active" />
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false" android:drawable="@drawable/home_normal" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@drawable/home_active" />
<item android:state_pressed="true" android:drawable="@drawable/home_normal" />
<item android:drawable="@drawable/home_normal" />
</selector>
一般都加上最后一个,以防前面没列举全最后显示出错
然后在建tab时加上,因为setIndicator 有3个重载,可以用的参数方式是:(CharSequence label) 一个字符串;(CharSequence label, Drawable icon) 字符串加图标;(View view) ,一个图像,就可以复杂点了,然后还是用Inflate从xml文件中得到,代码如下:
//加上首页的Activity
View view1 = (View)LayoutInflater.from(MainActivity.this).inflate(R.layout.tabitem, null);
((ImageView)view1.findViewById(R.id.tabimage)).setImageResource(R.drawable.hometab);
((TextView)view1.findViewById(R.id.tabtitle)).setText(WEIBO_HOME);
it = new Intent(MainActivity.this,HomeTimeLineActivity.class);
tspec = tabhost.newTabSpec(WEIBO_HOME).setIndicator(view1).setContent(it);
tabhost.addTab(tspec);
tabitem.xml很简单,就是个ImageView加TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/tabimage"
android:layout_width="fill_parent" android:layout_height="32.0dip"
android:scaleType="fitCenter" />
<TextView android:id="@+id/tabtitle"
android:textSize="11.0sp" android:gravity="center"
android:layout_width="fill_parent" android:layout_height="wrap_content"
/>
</LinearLayout>
最终效果如下:
效果还是一般,美工不好做啊,图片太难找啊