Android 分页控件制成底部菜单.
16lz
2021-01-23
其实Android 中的底部菜单, 可以用分页控件很好的实现。 我们先将自定义分页控件做好, 就可以做到顶底两个位置的菜单了。
TabHost只是作为一个容器来存放一些Activity, 所以需要自己另外创建几个新的Activity, 然后由主TabHost加载。
tab_style.xml
是每个Tab的自定义样式
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //分页控件样式 <? 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 > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | //TabHost布局 <? xml version = "1.0" encoding = "UTF-8" ?> < TabHost xmlns:android = "http://schemas.android.com/apk/res/android" android:id = "<a href=" http://my.oschina.net/asia" class = "referer" target = "_blank" >@android</ a > :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 = "<a href=" http://my.oschina.net/asia" class = "referer" target = "_blank" >@android</ a > :id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" /> //TabWidget位置在FrameLayout之下则显示在低部, 在之上则显示在顶部 < TabWidget android:id = "<a href=" http://my.oschina.net/asia" class = "referer" target = "_blank" >@android</ a > :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的按下背景
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //选择器,指示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类即可, 在此不显示。
?1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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使用DrawerLayout实现抽屉式侧滑菜单
- android 控件学习---------GridView
- 自定义SeekBar样式
- Android 控件之Gallery和ImageSwitcher图片切换器
- 【Android 设计】:样式_ 图解
- Android修改字体样式的示例代码
- Android中常常使用shape来定义控件
- 关于解决为什么设置控件居中等位置无反应的问题