主要布局:
package com.test.activity; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.Window; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TabHost; import com.test.testbottombar.R; /** * MainTabAcitivity * @author LinHang_He * */ public class MainTabActivity extends TabActivity implements OnCheckedChangeListener{ private RadioGroup mainTab; private TabHost mTabHost; //内容Intent private Intent mHomeIntent; private Intent mNewsIntent; private Intent mInfoIntent; private Intent mSearchIntent; private Intent mMoreIntent; private final static String TAB_TAG_HOME="tab_tag_home"; private final static String TAB_TAG_NEWS="tab_tag_news"; private final static String TAB_TAG_INFO="tab_tag_info"; private final static String TAB_TAG_SEARCH="tab_tag_search"; private final static String TAB_TAG_MORE="tab_tag_more"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); mainTab=(RadioGroup)findViewById(R.id.main_tab); mainTab.setOnCheckedChangeListener(this); prepareIntent(); setupIntent(); } /** * 准备tab的内容Intent */ private void prepareIntent() { //mHomeIntent=new Intent(this, HomeActivity.class); mHomeIntent=new Intent(this, FriendsDynamicActivity.class); mNewsIntent=new Intent(this, InfoActivity.class); mInfoIntent=new Intent(this, FriendsOrGroupActivity.class); mSearchIntent=new Intent(this,SquareActivity.class); mMoreIntent=new Intent(this, MoreActivity.class); } /** * */ private void setupIntent() { this.mTabHost=getTabHost(); TabHost localTabHost=this.mTabHost; localTabHost.addTab(buildTabSpec(TAB_TAG_HOME, R.string.main_home, R.drawable.icon_1_n, mHomeIntent)); localTabHost.addTab(buildTabSpec(TAB_TAG_NEWS, R.string.main_news, R.drawable.icon_2_n, mNewsIntent)); localTabHost.addTab(buildTabSpec(TAB_TAG_INFO, R.string.main_my_info, R.drawable.icon_3_n, mInfoIntent)); localTabHost.addTab(buildTabSpec(TAB_TAG_SEARCH, R.string.menu_search, R.drawable.icon_4_n, mSearchIntent)); localTabHost.addTab(buildTabSpec(TAB_TAG_MORE, R.string.more, R.drawable.icon_5_n, mMoreIntent)); } private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,final Intent content) { return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel), getResources().getDrawable(resIcon)).setContent(content); } public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.radio_button0: this.mTabHost.setCurrentTabByTag(TAB_TAG_HOME); break; case R.id.radio_button1: this.mTabHost.setCurrentTabByTag(TAB_TAG_NEWS); break; case R.id.radio_button2: this.mTabHost.setCurrentTabByTag(TAB_TAG_INFO); break; case R.id.radio_button3: this.mTabHost.setCurrentTabByTag(TAB_TAG_SEARCH); break; case R.id.radio_button4: this.mTabHost.setCurrentTabByTag(TAB_TAG_MORE); break; } } }
创建需要的5个Activity
布局文件main.xml
<?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" android:background="#ffffff" > <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0"/> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" android:visibility="gone"/> <RadioGroup android:id="@+id/main_tab" android:background="@drawable/maintab_toolbar_bg" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_gravity="bottom"> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/main_home" android:drawableTop="@drawable/icon_1_n" android:id="@+id/radio_button0" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/main_news" android:drawableTop="@drawable/icon_2_n" android:id="@+id/radio_button1" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/main_my_info" android:drawableTop="@drawable/icon_3_n" android:id="@+id/radio_button2" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/menu_search" android:drawableTop="@drawable/icon_4_n" android:id="@+id/radio_button3" style="@style/main_tab_bottom"/> <RadioButton android:layout_marginTop="2.0dip" android:text="@string/more" android:drawableTop="@drawable/icon_5_n" android:id="@+id/radio_button4" style="@style/main_tab_bottom"/> </RadioGroup> </LinearLayout> </TabHost>
创建drawable文件夹,添加home_btn_bg.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:state_enabled="true" android:state_pressed="false" android:drawable="@drawable/home_btn_bg_d"/> <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/home_btn_bg_d" /> <item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/home_btn_bg_d" /> <item android:drawable="@drawable/transparent" /> </selector>
在values文件夹下添加dimens.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="bottom_tab_padding_drawable">2.0dip</dimen> <dimen name="bottom_tab_padding_up">5.0dip</dimen> <dimen name="bottom_tab_font_size">10.0dip</dimen> </resources>
在values文件夹下添加drawables.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <item type="drawable" name="transparent">#00000000</item> </resources>
strings.xml
<resources> <string name="app_name">TestBottomBar</string> <string name="main_home">首页</string> <string name="main_news">新闻</string> <string name="main_my_info">信息</string> <string name="menu_search">搜索</string> <string name="more">更多</string> </resources>
styles.xml
<resources> <style name="AppTheme" parent="android:Theme.Light" /> <style name="main_tab_bottom"> <item name="android:textSize">@dimen/bottom_tab_font_size</item> <item name="android:textColor">#ffffffff</item> <item name="android:ellipsize">marquee</item> <item name="android:gravity">center_horizontal</item> <item name="android:background">@drawable/home_btn_bg</item> <item name="android:paddingTop">@dimen/bottom_tab_padding_up</item> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:button">@null</item> <item name="android:singleLine">true</item> <item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item> <item name="android:layout_weight">1.0</item> </style> </resources>
相关推荐
开源项目BottomBar,实现Android底部菜单(常用菜单,BottomBar实现动画(上下式)+消息菜单,BottomBar+ViewPager+Fragment实现炫酷的底部导航效果)
Github 开源项目BottomBar 非常炫酷好用的底部菜单栏
来源于GitHub大神的轮子,源地址https://github.com/roughike/BottomBar 配合博客BottomBar学习使用
以前用fragment都喜欢用support.v4.app.Fragment 这是为了兼容3.0以下,又搓又不好用, 其实现在不用 3.0以下的手机都已经没人用了。直接用app.fragment即可。该源码以最简单的方式实现了底部导航菜单。
博客中关于BottomBar的相关代码:http://blog.csdn.net/fessible_max/article/details/72802977
BottomBar Version 2.0 released! The latest version before that can be found in the v1 branch Cleaner code and better APIs No more unnecessary stuff or spaghetti mess Now the look, feel and ...
实现android炫酷的底部导航效果
一个比较炫的安卓底部导航控件。还包含了supper v7包和design包。
这是Android的主题通用框架,就是上面是页面切换,下面是几个按钮,切换到不同的fragment里面,里面做了,fragment的重叠,和销毁时的问题。
底部栏 适用于 Android 的独立 BottomBar 库
于是就用这种方式实现了,效果还不错。github有详细说明,多余的就不说了。 这个roughike是这个项目的所有者(大神致敬)。 我用的是Android studio开发,fragment全部导的V4的包(以为最开始就支持的是v4的,...
受此概念启发的SwiftUI的BottomBar组件受此概念启发的SwiftUI的BottomBar组件要求Xcode 11 iOS 13安装在Xcode中,转到“文件”->“ Swift软件包”->“添加软件包依赖性”,然后粘贴此回购网址https:// github。...
BottomBar使用 开源地址:https://github.com/roughike/BottomBar PS:如果觉得文章太长,你也可观看该课程的视频,亲,里面还有高清,无码的福利喔 运行效果 使用步骤 1. 在Module的build.gradle添加依赖 compile...
bottomBar app底部导航栏 使用: 添加依赖 1.项目gradle添加一下配置: allprojects { repositories { ... maven { url 'https://jitpack.io' } } } 2.module中的gradle添加依赖: dependencies { ...
bottombar:仿闲鱼底部导航支持本地和网络图片
安装 添加依赖项: Pure React Native: npm i react - native - bottom - bar 博览会版本: ... BottomBar style = { style } shapeColor = { shapeColor } mainIcon = { mainIcon } mainIconColor =
本文给大家介绍在Android中如何实现顶部导航菜单左右滑动效果,具体内容如下 第一种解决方案: 实现原理是使用android-support-v4.jar包中ViewPager控件,在ViewPager控件中设置流布局,再在流布局中设置几项...