实现屏幕下方展示的TAB分页
这篇博客是参考helloandroid兄的腾讯微博应用,我整理了一下。完整项目的介绍请在http://helloandroid.iteye.com/看
首先是效果图:
我把helloandroid兄的源代码整理了一下,并梳理了涉及到的知识点,总结如下:
1、TabActivity的使用
public class MainActivity extends TabActivity {private TabHost tabHost;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);tabHost = getTabHost();populateTab();}/** * 组装tab控件 */private void populateTab() {Resources res = getResources();populateTabItem(R.drawable.tab_home_selector,res.getString(R.string.tab_home), new Intent(this,HomeActivity.class));populateTabItem(R.drawable.tab_atme_selector,res.getString(R.string.tab_refer), new Intent(this,ReferActivity.class));populateTabItem(R.drawable.tab_message_selector,res.getString(R.string.tab_secret), new Intent(this,MessageActivity.class));populateTabItem(R.drawable.tab_explore_selector,res.getString(R.string.tab_search), new Intent(this,SearchActivity.class));populateTabItem(R.drawable.tab_focus_selector,res.getString(R.string.tab_attention), new Intent(this,AttentionActivity.class));}/** * 生成tab_item * * @param imageResourceSelector * 图片选择器 * @param text * 文本 * @param intent * intent */private void populateTabItem(int imageResourceSelector, String text,Intent intent) {View view = View.inflate(this, R.layout.tab_item, null);// 拼装view((ImageView) view.findViewById(R.id.tab_item_imageview)).setImageResource(imageResourceSelector);((TextView) view.findViewById(R.id.tab_item_textview)).setText(text);TabSpec spec = tabHost.newTabSpec(text).setIndicator(view).setContent(intent);// 将view注入spectabHost.addTab(spec);}}<?xml version="1.0" encoding="UTF-8"?><TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><RelativeLayout android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"><include android:id="@+id/head_line" layout="@layout/head_line"android:layout_width="fill_parent" android:layout_height="wrap_content" /><FrameLayout android:id="@android:id/tabcontent"android:layout_below="@id/head_line" android:layout_width="fill_parent"android:layout_height="fill_parent" android:layout_weight="1.0" /><TabWidget android:id="@android:id/tabs" android:background="@drawable/tab_bkg"android:layout_gravity="bottom" android:layout_height="60.0dip"android:layout_width="fill_parent" android:fadingEdge="none"android:fadingEdgeLength="0.0px" android:paddingLeft="0.0dip"android:paddingTop="2.0dip" android:paddingRight="0.0dip"android:paddingBottom="0.0dip" android:layout_alignParentBottom="true"android:layout_alignParentTop="false" /></RelativeLayout></TabHost>
<include android:id="@+id/head_line" layout="@layout/head_line"android:layout_width="fill_parent" android:layout_height="wrap_content" />
<RelativeLayout android:background="@drawable/header"android:layout_width="fill_parent" android:layout_height="wrap_content"xmlns:android="http://schemas.android.com/apk/res/android"><Button android:id="@+id/top_btn_left" android:textColor="@color/button_text_selector"android:background="@drawable/top_refresh_selector"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_marginLeft="12.0dip" android:layout_alignParentLeft="true"android:layout_centerVertical="true" /><Button android:id="@+id/top_btn_right" android:textColor="@color/button_text_selector"android:background="@drawable/top_edit_selector" android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_marginRight="12.0dip"android:layout_alignParentRight="true" android:layout_centerVertical="true" /><TextView android:id="@+id/top_title" android:textSize="22.0sp"android:textColor="@color/head_line_text" android:ellipsize="middle"android:gravity="center_horizontal" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="@string/user_name"android:singleLine="true" android:layout_toLeftOf="@id/top_btn_right"android:layout_toRightOf="@id/top_btn_left"android:layout_centerInParent="true"android:layout_alignWithParentIfMissing="true" /></RelativeLayout>
<?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/tab_item_imageview"android:layout_width="fill_parent" android:layout_height="32.0dip"android:scaleType="fitCenter" /><TextView android:id="@+id/tab_item_textview"android:layout_width="fill_parent" android:layout_height="wrap_content"android:gravity="center" android:singleLine="true"android:marqueeRepeatLimit="1" android:textSize="11.0sp"android:ellipsize="marquee" /></LinearLayout>
View view = View.inflate(this, R.layout.tab_item, null);// 拼装view((ImageView) view.findViewById(R.id.tab_item_imageview)).setImageResource(imageResourceSelector);((TextView) view.findViewById(R.id.tab_item_textview)).setText(text);TabSpec spec = tabHost.newTabSpec(text).setIndicator(view).setContent(intent);// 将view注入spectabHost.addTab(spec);
android:textColor="@color/button_text_selector"
<?xml version="1.0" encoding="UTF-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:color="@color/button_text_pressed" /><item android:state_selected="true" android:color="@color/button_text_pressed" /><item android:color="@color/button_text_normal" /></selector>
<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/top_edit_normal" /> <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/top_edit_press" /> <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/top_edit_normal" /> <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/top_edit_press" /> <item android:state_pressed="true" android:drawable="@drawable/top_edit_press" /></selector>
<RelativeLayout android:background="@drawable/header"android:layout_width="fill_parent" android:layout_height="wrap_content"xmlns:android="http://schemas.android.com/apk/res/android">
<?xml version="1.0" encoding="UTF-8"?><shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"><gradient android:startColor="#ff6c7078" android:endColor="#ffa6abb5"android:angle="270.0" android:type="linear" /></shape>