android 开发实例 底部导航栏(1)
1.使用LinearLayout 底部布局+imageView 实现
底部四个主导航页面 布局文件 activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.tastelibrary.MainActivity"tools:ignore="MergeRootFrame" ><FrameLayoutandroid:id="@+id/fl_content"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" ></FrameLayout><LinearLayoutandroid:id="@+id/ll"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><LinearLayoutandroid:id="@+id/ll_recipe"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:orientation="vertical" ><ImageViewandroid:id="@+id/image_recipe"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/recipe_btn_selector" /></LinearLayout><LinearLayoutandroid:id="@+id/ll_kitchen"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:orientation="vertical" ><ImageViewandroid:id="@+id/image_kitchen"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/kitchen_btn_selector" /></LinearLayout><LinearLayoutandroid:id="@+id/ll_find"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:orientation="vertical" ><ImageViewandroid:id="@+id/image_find"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/find_btn_selector" /></LinearLayout><LinearLayoutandroid:id="@+id/ll_user"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:orientation="vertical" ><ImageViewandroid:id="@+id/image_user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/user_btn_selector" /></LinearLayout></LinearLayout></LinearLayout>
MainActivity
package com.example.tastelibrary;import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.os.Build;public class MainActivity extends ActionBarActivity implements OnClickListener{private LinearLayout ll_recipe; private LinearLayout ll_kitchen; private LinearLayout ll_find; private LinearLayout ll_user; private ImageView image_home; private ImageView image_friends; private ImageView image_message; private ImageView image_more; //Fragment管理器 private FragmentManager fm = this.getSupportFragmentManager(); private FragmentTransaction ft; private RecipeFragment fragmentPage1; private FindFragment fragmentPage2; private KitchenFragment fragmentPage3; private UserFragment fragmentPage4; ActionBar myActionBar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myActionBar=getSupportActionBar();initView(); //开始事务(每次改变Fragment管理器之后都要提交) ft = fm.beginTransaction(); home(); //提交事务 ft.commit(); }private void initView(){ ll_recipe = (LinearLayout)findViewById(R.id.ll_recipe); ll_kitchen = (LinearLayout)findViewById(R.id.ll_kitchen); ll_find = (LinearLayout)findViewById(R.id.ll_find); ll_user = (LinearLayout)findViewById(R.id.ll_user); image_home = (ImageView)findViewById(R.id.image_recipe); image_friends = (ImageView)findViewById(R.id.image_kitchen); image_message = (ImageView)findViewById(R.id.image_find); image_more = (ImageView)findViewById(R.id.image_user); ll_recipe.setOnClickListener(this); ll_kitchen.setOnClickListener(this); ll_find.setOnClickListener(this); ll_user.setOnClickListener(this); ll_recipe.setSelected(true); image_home.setSelected(true); } @Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}@Overridepublic void onClick(View v) {//每次点击时都需要重新开始事务 ft = fm.beginTransaction(); //把显示的Fragment隐藏 setSelected(); switch (v.getId()) { case R.id.ll_recipe: ll_recipe.setSelected(true); image_home.setSelected(true); home(); break; case R.id.ll_kitchen: ll_kitchen.setSelected(true); image_friends.setSelected(true); friend(); break; case R.id.ll_find: ll_find.setSelected(true); image_message.setSelected(true); message(); break; case R.id.ll_user: ll_user.setSelected(true); image_more.setSelected(true); more(); break; } ft.commit(); }/*** 设置每个按钮是否被选中*/private void setSelected(){ ll_recipe.setSelected(false); ll_kitchen.setSelected(false); ll_find.setSelected(false); ll_user.setSelected(false); image_home.setSelected(false); image_friends.setSelected(false); image_message.setSelected(false); image_more.setSelected(false); if(fragmentPage1 != null){ //隐藏Fragment ft.hide(fragmentPage1); } if(fragmentPage2 != null){ ft.hide(fragmentPage2); } if(fragmentPage3 != null){ ft.hide(fragmentPage3); } if(fragmentPage4 != null){ ft.hide(fragmentPage4); } } private void home(){ if(fragmentPage1 == null){ fragmentPage1 = new RecipeFragment(); /*添加到Fragment管理器中 这里如果用replace, 当每次调用时都会把前一个Fragment给干掉, 这样就导致了每一次都要创建、销毁, 数据就很难保存,用add就不存在这样的问题了, 当Fragment存在时候就让它显示,不存在时就创建, 这样的话数据就不需要自己保存了, 因为第一次创建的时候就已经保存了, 只要不销毁一直都将存在*/ ft.add(R.id.fl_content, fragmentPage1); }else{ //显示Fragment ft.show(fragmentPage1); } } private void friend(){ if(fragmentPage2 == null){ fragmentPage2 = new FindFragment(); ft.add(R.id.fl_content, fragmentPage2); }else{ ft.show(fragmentPage2); } } private void message(){ if(fragmentPage3 == null){ fragmentPage3 = new KitchenFragment(); ft.add(R.id.fl_content, fragmentPage3); }else{ ft.show(fragmentPage3); } } private void more(){ if(fragmentPage4 == null){ fragmentPage4 = new UserFragment(); ft.add(R.id.fl_content, fragmentPage4); }else{ ft.show(fragmentPage4); } }
}
2.RadioButton 实现
布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"tools:context=".MainActivity" ><RelativeLayoutandroid:id="@+id/aboveLayout"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/white"android:orientation="vertical" ><FrameLayoutandroid:id="@+id/fragmentRoot"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_alignParentLeft="true"android:orientation="vertical" ></FrameLayout><LinearLayoutandroid:id="@+id/bottomList"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:background="#bbbbbb"android:orientation="horizontal" ><RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:background="#bbbbbb"android:gravity="center_vertical"android:orientation="horizontal" ><RadioButtonandroid:id="@+id/birtthday"style="@style/footbar"android:drawableTop="@drawable/birthday_normal"android:height="25dp"android:paddingTop="8dp"android:text="生日"android:width="40dp" /><RadioButtonandroid:id="@+id/message"style="@style/footbar"android:drawableTop="@drawable/message_normal"android:height="25dp"android:paddingTop="8dp"android:text="祝福短信"android:width="40dp" /><RadioButtonandroid:id="@+id/note"style="@style/footbar"android:drawableTop="@drawable/ej_label_add_d"android:height="25dp"android:paddingTop="8dp"android:text="生日记事"android:width="40dp" /><RadioButtonandroid:id="@+id/me"style="@style/footbar"android:drawableTop="@drawable/me_normal"android:height="25dp"android:paddingTop="8dp"android:text="我的"android:width="40dp" /></RadioGroup></LinearLayout></RelativeLayout></RelativeLayout></RelativeLayout>
MainActivity
package com.gemptc.birthdaydemonapp.activity;import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.Toast;import com.gemptc.birthdaydemonapp.R;
import com.gemptc.birthdaydemonapp.fragment.BirthdayFragment;
import com.gemptc.birthdaydemonapp.fragment.MeFragment;
import com.gemptc.birthdaydemonapp.fragment.MessageFragment;
import com.gemptc.birthdaydemonapp.fragment.NoteFragment;
import com.gemptc.birthdaydemonapp.sqlite.DBManger;
import com.zhe.rex.*;public class MainActivity extends ActionBarActivity {private ActionBar actionbar;private static FragmentManager fMgr;private Eoiu spotManager;private RadioButton birthday;private RadioButton message;private RadioButton blesswall;private RadioButton me;private int i = 1;private NoteFragment nf = null;private MessageFragment mf;private BirthdayFragment bf;private MeFragment mf1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);actionbar = getSupportActionBar();actionbar.hide();birthday = (RadioButton) findViewById(R.id.birtthday);message = (RadioButton) findViewById(R.id.message);me = (RadioButton) findViewById(R.id.me);// 获取FragmentManager实例fMgr = getSupportFragmentManager();initFragment();dealBottomButtonsClickEvent();}/*** 初始化首个Fragment*/private void initFragment() {FragmentTransaction ft = fMgr.beginTransaction();bf = new BirthdayFragment();ft.add(R.id.fragmentRoot, bf, "birthdayFragment");ft.addToBackStack(null);ft.commit();}/*** 处理底部点击事件*/private void dealBottomButtonsClickEvent() {findViewById(R.id.birtthday).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {FragmentTransaction ft = fMgr.beginTransaction();ft.replace(R.id.fragmentRoot, bf, "birthdayFragment");ft.addToBackStack(null);ft.commit();}});findViewById(R.id.message).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {FragmentTransaction ft = fMgr.beginTransaction();if (mf == null) {mf = new MessageFragment();}ft.replace(R.id.fragmentRoot, mf, "MessageFragment");ft.addToBackStack(null);ft.commit();}});findViewById(R.id.note).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {FragmentTransaction ft = fMgr.beginTransaction();if (nf == null) {nf = new NoteFragment();}ft.replace(R.id.fragmentRoot, nf, "NoteFragment");ft.addToBackStack(null);ft.commit();}});findViewById(R.id.me).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {FragmentTransaction ft = fMgr.beginTransaction();if (mf1 == null) {mf1 = new MeFragment();}ft.replace(R.id.fragmentRoot, mf1, "MeFragment");ft.addToBackStack(null);ft.commit();}});}// 点击返回按钮@Overridepublic void onBackPressed() {i++;if (i == 2) {Toast.makeText(this, "再按一次退出", Toast.LENGTH_LONG).show();Eoiu spotManager = Eoiu.getInstance(getApplicationContext(),"bec9e95da11570a46e46b2b005c204a9");spotManager.exit(this);}elseMainActivity.this.finish();}@Overrideprotected void onRestart() {i = 1;super.onRestart();}@Overrideprotected void onResume() {i = 1;super.onResume();}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {case Add_REQUEST_CODE:nf.refresh();break;default:break;
}super.onActivityResult(requestCode, resultCode, data);}}
未完待续 可以滑动的导航 viewPager+Fragment
源码
android 开发实例 底部导航栏(1)相关推荐
- Android开发之底部导航栏标准
以下是封装的库源码: 1 package com.example.oldtab; 2 3 import java.util.ArrayList; 4 5 import android.content. ...
- android仿微信的activity平滑水平切换动画,Android实现简单底部导航栏 Android仿微信滑动切换效果...
Android实现简单底部导航栏 Android仿微信滑动切换效果 发布时间:2020-10-09 19:48:00 来源:脚本之家 阅读:96 作者:丶白泽 Android仿微信滑动切换最终实现效果 ...
- 微信小程序开发错误——底部导航栏没有显示完全
微信小程序开发错误--底部导航栏没有显示完全 原因:由于在app.json中设置跳转页面时,假设有A.B.C.D四个图标(点击可以跳转),B和C的跳转页面相同,C的图标就会覆盖B的图标,B的图标就相当 ...
- android仿咸鱼底部导航栏,Flutter沉浸式状态栏/AppBar导航栏/仿咸鱼底部凸起导航栏效果...
如下图:状态栏是指android手机顶部显示手机状态信息的位置. android 自4.4开始新加入透明状态栏功能,状态栏可以自定义颜色背景,使titlebar能够和状态栏融为一体,增加沉浸感. 如上 ...
- php仿微信底部菜单,Android实现简单底部导航栏 Android仿微信滑动切换效果
Android仿微信滑动切换最终实现效果: 大体思路: 1. 主要使用两个自定义View配合实现; 底部图标加文字为一个自定义view,底部导航栏为一个载体,根据需要来添加底部图标; 2. 底部导航栏 ...
- android滑动菜单图标,Android实现简单底部导航栏 Android仿微信滑动切换效果
Android仿微信滑动切换最终实现效果: 大体思路: 1. 主要使用两个自定义View配合实现; 底部图标加文字为一个自定义view,底部导航栏为一个载体,根据需要来添加底部图标; 2. 底部导航栏 ...
- android 底部滑动效果怎么做,Android实现简单底部导航栏 Android仿微信滑动切换效果...
android仿微信滑动切换最终实现效果: 大体思路: 1. 主要使用两个自定义view配合实现; 底部图标加文字为一个自定义view,底部导航栏为一个载体,根据需要来添加底部图标; 2. 底部导航栏 ...
- JFTabBar android强大的底部导航栏框架 (微信底部导航栏效果)
TabBar这个名字相信很多学过一点IOS程序员都知道它是用来干嘛的,但本人也并非擅长开发IOS程序员,只是略懂略懂....这是一个很强大的TabBar,可满足很多需求.用起来也非常简单,在oncre ...
- 开发vue底部导航栏组件
这个想法源于最近自己在开发一个移动端博客的一个底部导航栏,原型设计如下: 我们来分析一下这个导航栏,其实很简单啦,就是自适应固定在底部 我们可以使用CSS3属性display:flex设置父级盒子为伸 ...
最新文章
- 徐匡迪、潘云鹤等纷纷撰文,关于人工智能的最新判断都在这里了
- QT中父子窗口事件传递与事件过滤器
- JS中对象创建的五中方式
- 【设计素材】表格数据形平面海报素材
- 只想着一直调用一直爽, 那API凭证泄漏风险如何破?
- VirtualBox的BUG:没超线程也认为有
- Verilog 教程
- STM32+DWM1000开发uwb测距系列教程之二:源码分析及源码移植(基于STM32 cubemx+keil MDK)
- Linux基础知识点总结
- badbody下_BadboyInstaller下载-录制脚本工具Badboy下载2.2.5 官方最新版-西西软件下载...
- 旅游业休克:“云旅游+直播买货”急救
- Android 打开应用商店评分
- Eclips注释模板的使用
- 【算法分析】多个对比算法的统计检验方法
- XXE外部实体注入漏洞总结
- python进行随机数据生成——Faker的使用
- [转]目前游戏行业内部主要几款游戏引擎的技术对比
- python收集参数_python收集参数
- Win10 提示你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问
- Ph.D Grind 读后感 —— 张宇鹏