安卓开发笔记——TabHost组件(二)(实现底部菜单导航)
2014-09-16 15:51
746 查看
上面文章《安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)》中提到了利用自定义View(ImageView+TextView)来设置一个底部菜单的样式
这边再补充一种更为灵活的方法,可以把TabWidget隐藏,用(RadioGroup+RadioButton)来代替,并利用监听器的方式来实现监听点击点击跳转Activity。
在讲解之前,先补充几点:
1、当我们取得TabHost的实例对象时,我们可以用2种方法来设置当前界面内容(Activity)
查看下API,我们可以找到:
这2个方法,分别是利用页面标志符(int,起始页为0)和页面标签(String)来达到设置当前显示页
我们可以利用这2个方法,在对Radio进行监听点击时跳转到对应的页面。
2、在布局文件XML里,标签RadioGroup的子标签RadioButton里有几个需要注意的地方:
1、android:button="@null" 这个是用来隐藏系统自身提供的单选按钮样式
2、android:drawableTop="@drawable/tab_icon1" 这个是用来设置单选按钮的图标(系统默认提供在左边,这个可以把它设置在上面)
3、android:drawablePadding="3dp" 这个是用来设置按钮图标与按钮标签所在的距离
3、需要把TabWidget设置为隐藏:android:visibility="gone"
为什么说采用这种方法更为灵活呢?
看过我上一篇文章的朋友应该都知道,在上一篇文章里我才采用了自定义的代码布局,需要在JAVA代码里去获取XML布局对象,然后对样式进行设置,而现在利用RadioGroup+RadioButton,我们可以把所有的样式都设置在XML布局文件里,这样更易于维护,在JAVA代码里我们可以更加专注于业务逻辑的代码实现。
好了,接下来上代码吧,由于和上个例子几乎一样,重复部分我就不贴出来了,只贴代码核心部分(注释很全)
这边再补充一种更为灵活的方法,可以把TabWidget隐藏,用(RadioGroup+RadioButton)来代替,并利用监听器的方式来实现监听点击点击跳转Activity。
在讲解之前,先补充几点:
1、当我们取得TabHost的实例对象时,我们可以用2种方法来设置当前界面内容(Activity)
查看下API,我们可以找到:
这2个方法,分别是利用页面标志符(int,起始页为0)和页面标签(String)来达到设置当前显示页
我们可以利用这2个方法,在对Radio进行监听点击时跳转到对应的页面。
2、在布局文件XML里,标签RadioGroup的子标签RadioButton里有几个需要注意的地方:
1、android:button="@null" 这个是用来隐藏系统自身提供的单选按钮样式
2、android:drawableTop="@drawable/tab_icon1" 这个是用来设置单选按钮的图标(系统默认提供在左边,这个可以把它设置在上面)
3、android:drawablePadding="3dp" 这个是用来设置按钮图标与按钮标签所在的距离
3、需要把TabWidget设置为隐藏:android:visibility="gone"
为什么说采用这种方法更为灵活呢?
看过我上一篇文章的朋友应该都知道,在上一篇文章里我才采用了自定义的代码布局,需要在JAVA代码里去获取XML布局对象,然后对样式进行设置,而现在利用RadioGroup+RadioButton,我们可以把所有的样式都设置在XML布局文件里,这样更易于维护,在JAVA代码里我们可以更加专注于业务逻辑的代码实现。
好了,接下来上代码吧,由于和上个例子几乎一样,重复部分我就不贴出来了,只贴代码核心部分(注释很全)
package com.example.tabhosttest; import android.app.ActivityGroup; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TabHost; import android.widget.TabHost.TabSpec; import android.widget.TextView; public class MainActivity extends ActivityGroup{ private TabHost tabHost;//声明一个TabHost对象 private RadioGroup radioGroup;//声明一个RadioGroup对象 //资源文件 private Class activitys[]={TabActivity1.class,TabActivity2.class,TabActivity3.class,TabActivity4.class,TabActivity5.class};//跳转的Activity private String title[]={"首页","搜索","设置","主题","更多"};//设置菜单的标题 private int image[]={R.drawable.tab_icon1,R.drawable.tab_icon2,R.drawable.tab_icon3,R.drawable.tab_icon4,R.drawable.tab_icon5,};//设置菜单 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initTabView();//初始化tab标签 } private void initTabView() { //实例化tabhost this.tabHost=(TabHost) findViewById(R.id.mytabhost); //由于继承了ActivityGroup,所以需要在setup方法里加入此参数,若继承TabActivity则可省略 tabHost.setup(this.getLocalActivityManager()); //创建标签 for(int i=0;i<activitys.length;i++){ /*由于采用了RadioGroup,样式已经在xml里设置,故这里无需再自定义view //实例化一个view作为tab标签的布局 View view=View.inflate(this, R.layout.tab_layout, null); //设置imageview ImageView imageView=(ImageView) view.findViewById(R.id.image); imageView.setImageDrawable(getResources().getDrawable(image[i])); //设置textview TextView textView=(TextView) view.findViewById(R.id.title); textView.setText(title[i]); */ //设置跳转activity Intent intent=new Intent(this, activitys[i]); //载入view对象并设置跳转的activity TabSpec spec=tabHost.newTabSpec(title[i]).setIndicator(title[i]).setContent(intent); //添加到选项卡 tabHost.addTab(spec); } radioGroup=(RadioGroup) findViewById(R.id.radiogroup); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.radio1: //tabHost.setCurrentTab(int id); 用当前所在页数来跳转activity //tabHost.setCurrentTabByTag(String tag); 用当前标签来跳转activity tabHost.setCurrentTabByTag(title[0]); break; case R.id.radio2: tabHost.setCurrentTabByTag(title[1]); break; case R.id.radio3: tabHost.setCurrentTabByTag(title[2]); break; case R.id.radio4: tabHost.setCurrentTabByTag(title[3]); break; case R.id.radio5: tabHost.setCurrentTabByTag(title[4]); break; } } }); } }
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mytabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 需要一个布局管理器 --> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 由于TabHost是继承于FrameLayout,所以需要一个FrameLaytout布局(内容页) ,id 必须为tabcontent --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" > </FrameLayout> <!-- TabWidget必须标签,用来存放tab标签,且id必须为tabs --> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/tab_widget_background" android:visibility="gone" > </TabWidget> <RadioGroup android:id="@+id/radiogroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/tab_widget_background" android:orientation="horizontal" android:padding="3dp" > <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_selector" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/tab_icon1" android:gravity="center_horizontal" android:text="首页" android:textColor="@android:color/white" android:textSize="10sp" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_selector" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/tab_icon2" android:gravity="center_horizontal" android:text="搜索" android:textColor="@android:color/white" android:textSize="10sp" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_selector" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/tab_icon3" android:gravity="center_horizontal" android:text="设置" android:textColor="@android:color/white" android:textSize="10sp" /> <RadioButton android:id="@+id/radio4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_selector" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/tab_icon4" android:gravity="center_horizontal" android:text="主题" android:textColor="@android:color/white" android:textSize="10sp" /> <RadioButton android:id="@+id/radio5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_selector" android:button="@null" android:drawablePadding="3dp" android:drawableTop="@drawable/tab_icon5" android:gravity="center_horizontal" android:text="更多" android:textColor="@android:color/white" android:textSize="10sp" /> </RadioGroup> </RelativeLayout> </TabHost>
相关文章推荐
- 安卓开发笔记——TabHost组件(一)(实现底部菜单导航)
- 安卓开发复习笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)
- 安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)
- 转-TabHost组件(一)(实现底部菜单导航)
- 转-TabHost组件(二)(实现底部菜单导航)
- 安卓开发复习笔记——Fragment+FragmentTabHost组件(实现新浪微
- 安卓开发复习笔记——Fragment+FragmentTabHost组件(实现新浪微
- 【安卓】安卓App开发思路 一步一个脚印(二)FragmentTabHost实现底部的菜单
- 安卓从零开发之购物商城(一)--底部菜单栏的的实现(FragmentTabHost)
- TabHost、RadioGroup、ToolBar实现底部菜单导航
- TabHost、RadioGroup实现底部菜单导航
- 安卓开发笔记——多种方式实现底部菜单栏(仿微信界面)
- TabHost、RadioGroup实现底部菜单导航
- 安卓开发笔记——关于开源组件PullToRefresh实现下拉刷新和上拉加载(一分钟搞定,超级简单)
- 安卓开发笔记——Menu菜单组件(选项菜单,上下文菜单,子菜单)
- 转-Fragment+FragmentTabHost组件(实现新浪微博底部菜单)
- 安卓开发:自定义PopupWindow,实现模仿iOS底部弹出菜单
- 安卓开发文档学习笔记之实现ActionBar中Tab导航
- 主流移动应用开发框架(2)——fragment+fragmenttabhost实现底部选项卡导航(可滑动切换)
- 安卓开发笔记——多种方式实现底部菜单栏(仿微信界面)