Android 分页控件制成底部菜单
Android 分页控件制成底部菜单
其实Android 中的底部菜单, 可以用分页控件很好的实现。 我们先将自定义分页控件做好, 就可以做到顶底两个位置的菜单了。
TabHost只是作为一个容器来存放一些Activity, 所以需要自己另外创建几个新的Activity, 然后由主TabHost加载。
tab_style.xml
是每个Tab的自定义样式
- //分页控件样式
- <?xml version="1.0" encoding="UTF-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="5dip"
- android:paddingRight="5dip"
- android:paddingTop="5dip"
- android:background="@drawable/tab_bg"
- >
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="0.6"
- >
- <TextView
- android:id="@+id/tab_label"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- android:background="@drawable/tab_title_selector"
- android:textColor="#FFFFFF"
- android:textStyle="bold"
- />
- </FrameLayout>
- </LinearLayout>
main_tab.xml 是主TabHost布局文件
- //TabHost布局
- <?xml version="1.0" encoding="UTF-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- //必须包含下列三个View
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <FrameLayout
- android:gravity="center"
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- />
- //TabWidget位置在FrameLayout之下则显示在低部, 在之上则显示在顶部
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_weight="0.0"
- />
- </LinearLayout>
- </TabHost>
tab_title_selector.xml
是Tab中TextView的按下背景
- //选择器,指示Text按下后的背景
- <?xml version="1.0" encoding="UTF-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:state_focused="true"
- android:drawable="@drawable/tab_btn_bg_d"
- />
- <item
- android:state_selected="true"
- android:drawable="@drawable/tab_btn_bg_d"
- />
- <item
- android:state_pressed="true"
- android:drawable="@drawable/tab_btn_bg_d"
- />
- </selector>
Activity类
另外还需要几个Activity类, 普通的Activity类即可, 在此不显示。
- public class TabTest extends TabActivity
- {
- private TabWidget mTabWidget;
- private TabHost mTabHost;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_tabs);
- mTabHost = getTabHost();
- //将要显示的Activity载入TabHost控件
- //要显示的Activity由自己自由创建
- setTabIndicator("one", 1, new Intent(this, OneActivity.class));
- setTabIndicator("Two", 2, new Intent(this, TwoActivity.class));
- setTabIndicator("Three", 3, new Intent(this, OneActivity.class));
- setTabIndicator("Four", 4, new Intent(this, TwoActivity.class));
- }
- private void setTabIndicator(String title, int nId, Intent intent)
- {
- //使用指定Tab样式
- View view = LayoutInflater.from(this.mTabHost.getContext())
- .inflate(R.layout.tab_style, null);
- TextView text = (TextView)view.findViewById(R.id.tab_label);
- String strId = String.valueOf(nId);
- text.setText(title);
- //创建一个新Tab
- TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(strId)
- .setIndicator(view).setContent(intent);
- //加载新Tab
- mTabHost.addTab(localTabSpec);
- }
- }
Android 分页控件制成底部菜单源码下载地址:
免费下载地址在 http://linux.bkjia.com/
用户名与密码都是www.bkjia.com
具体下载目录在 /pub/Android源码集锦/2011年/12月/Android 分页控件制成底部菜单/
评论暂时关闭