Android 底部按钮BottomNavigationView + Fragment 的使用(二)
这里来试验BottomNavigationView + Fragment
底部按钮通过点击底部选项,实现中间的Fragment进行页面的切换。



使用BottomNavigationView 控件,实现底部按钮的布局,然后给按钮加上监听,监听选择后,实现中间Fragment页面的切换
上代码:found_main.xml 其他两个文件为menu_main.xml 和 user_main.xml ,从found_main.xml copy 过去就行,修改下里面的样式,便于区分。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent">
<!--用来显示具体内容--> <LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"> <TextView
android:id="@+id/titlename"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_weight="1"
android:background="#2b5361"
android:drawablePadding="3dp"
android:gravity="center"
android:text="发现" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="45dp"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2"> <ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/arrow_down_float" />
</LinearLayout> </android.support.constraint.ConstraintLayout>
创建主页面,activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MenuActivity"> <FrameLayout
android:id="@+id/FramePage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_purple"
app:layout_constraintBottom_toTopOf="@+id/viewline"> </FrameLayout> <View
android:id="@+id/viewline"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_above="@id/navigation"
android:background="@android:color/darker_gray"
app:layout_constraintBottom_toTopOf="@+id/navigation" /> <android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" /> </android.support.constraint.ConstraintLayout>
创建BottomNavigationView需要显示的item文件 nabigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" /> <item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" /> <item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications" /> </menu>
最后上主程序 MenuActivity.java
package action.sun.com.hello; import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TextView; import action.sun.com.hello.action.sun.com.hello.until.FirstFragment; public class MenuActivity extends AppCompatActivity { private String className= "MenuActivity";
// private ViewPager viewPager;
String msg = "Android : ";
//继承Activity 不会显示APP头上的标题
private FirstFragment f1,f2,f3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu); final BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation); //bottomNavigationView Item 选择监听
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Log.d("123", "onNavigationItemSelected is click: ");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
hideAllFragment(transaction);
switch (item.getItemId()){
case R.id.navigation_home:
Log.d(className, "R.id.navigation_home: ");
if(f1==null){
f1 = FirstFragment.newInstance("发现",R.layout.found_main);
transaction.add(R.id.FramePage,f1);
}else{
transaction.show(f1);
}
break;
case R.id.navigation_dashboard:
Log.d(className, "R.id.navigation_dashboard: ");
if(f2==null){
f2 =FirstFragment.newInstance("我的",R.layout.user_main);//"第二个Fragment"
transaction.add(R.id.FramePage,f2);
}else{
transaction.show(f2);
}
break;
case R.id.navigation_notifications:
Log.d(className, "R.string.title_notification: ");
if(f3==null){
f3 = FirstFragment.newInstance("关于",R.layout.menu_main);//"第三个Fragment"
transaction.add(R.id.FramePage,f3);
}else{
transaction.show(f3);
}
break;
}
transaction.commit();
Log.d(msg, "xxxxx "); return false;
}
}); } //隐藏所有Fragment
public void hideAllFragment(FragmentTransaction transaction){
if(f1!=null){
transaction.hide(f1);
}
if(f2!=null){
transaction.hide(f2);
}
if(f3!=null){
transaction.hide(f3);
} } }
FirstFragment.java
package action.sun.com.hello.action.sun.com.hello.until; import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Bundle; import action.sun.com.hello.R; public class FirstFragment extends Fragment{ private String context="xxxxxxxxxxxxx";
private TextView mTextView;
//要显示的页面
private int FragmentPage; public static FirstFragment newInstance(String context,int iFragmentPage){ FirstFragment myFragment = new FirstFragment();
myFragment.context = context;
myFragment.FragmentPage = iFragmentPage;
return myFragment;
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.context = context;
View view = inflater.inflate(FragmentPage,container,false);
//mTextView = (TextView)view.findViewById(R.id.titlename);
//mTextView = (TextView)getActivity().findViewById(R.id.txt_content);
///mTextView.setText(context);
//mTextView.setBackgroundColor(20);
return view;
}
}
到此,代码结束,可实现上面的效果。

Android 底部按钮BottomNavigationView + Fragment 的使用(二)的更多相关文章
- Android 底部按钮BottomNavigationView + Fragment + viewPager 的使用(一)
实现的效果,左右滑动,底部栏跟着滑动,中间加的是分帧的页面 上代码:主页面activity_main.xml <?xml version="1.0" encod ...
- Android中FragmentPagerAdapter对Fragment的缓存(二)
上一篇我们谈到了,当应用程序恢复时,由于FragmentPagerAdapter对Fragment进行了缓存的读取,导致其并未使用在Activity中新创建的Fragment实例.今天我们来看如何解决 ...
- Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像
此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...
- Android studio 基本布局-底部按钮
在使用Android studio 的时候,准备弄的基本的布局出来,底部按钮,按了中间会显示. 来上代码: 页面menu_main.xml 这里弄控件的浮动耗费了点我的时间.原因是因为对其各种问题, ...
- Android BottomSheet:底部弹出Fragment面板(4)
Android BottomSheet:底部弹出Fragment面板(4) BottomSheet不仅可以弹出轻量级的定制好的面板(见附录文章5,6,7),还可以弹出"重"的 ...
- Android UI-实现底部切换标签(fragment)
Android UI-实现底部切换标签(fragment) 前言 本篇博客要分享的一个UI效果--实现底部切换标签,想必大家在一些应用上面遇到过这样的效果了,最典型的就是微信了,能够左右滑动切换页面. ...
- Android组件内核之Fragment管理与内核(二)
阿里P7Android高级架构进阶视频免费学习请点击:https://space.bilibili.com/474380680本篇文章将先从以下三个内容来介绍Fragment管理与内核: [Fragm ...
- Android Fragment详解(二):Fragment创建及其生命周期
Fragments的生命周期 每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件. 对应生命周期可参考下图: 创建片元(Creating a Fragment) To c ...
- Android应用经典主界面框架之二:仿网易新闻client、CSDN client (Fragment ViewPager)
另外一种主界面风格则是以网易新闻.凤凰新闻以及新推出的新浪博客(阅读版)为代表.使用ViewPager+Fragment,即ViewPager里适配器里放的不是一般的View.而是Fragment.所 ...
随机推荐
- 【MySQL】MySQL之MySQL5.7中文乱码
自己的MySQL服务器不能添加中文,于是自己使用 show variables like 'character%'; 查看了当前的编码格式 我又通过以下方法将其设置为utf-8 SET charact ...
- Win7系统计算机中Msvcr100.dll丢失的解决办法
1.使用安全卫士里的人工服务. 在搜索框里输入msvcr100.dll. 点击查找方案. 2.点击msvcr100.dll问题后面的立即修复. 只要等待片刻就好了.
- Groovy 学习手册(4)
6. 领域特定语言 Groovy 有许多特性,使它非常适合写DSL(领域特定语言).这些特性包活: 具有委托机制的闭包: 点号(.)和语句末尾的分号(;)是可选的: 运算符的重载(例如,加号,减号等) ...
- 如何提升RDS响应速度
如果采用短连接的方式连接RDS,请在与RDS相连的云服务器上执行以下命令来提升RDS响应速度.(注:云服务器要有公网带宽) 1.安装nscd 云服务器上执行命令 Ubuntu系统执行:apt-get ...
- apache提示没有设置 max-age or expires解决办法
大家看到这个就应该知道只要设置 max-age or expires就行了.下面说的方法是在设置 apache下的方法: 产生要开启 代码如下 复制代码 LoadModule headers_modu ...
- [转]Java 变量和常量
变量和常量 在程序中存在大量的数据来代表程序的状态,其中有些数据在程序的运行过程中值会发生改变,有些数据在程序运行过程中值不能发生改变,这些数据在程序中分别被叫做变量和常量. 在实际的程序中,可以根据 ...
- chrome 版本 29.0.1547.76 m 解决打开新标签页后的恶心页面的问题
个人非常不喜欢这个版本的新标签页的样子,特别是一再输入框中输入要搜索的东西,自动跑到标题栏中去了,比吃屎还恶心.下面是解决办法: 在地址栏输入:chrome://flags/ 按Ctrl+F,输入下面 ...
- 如何使用Git 下载GitHub的东西
1. 先安装git 2. 注册一个github账号 3. 新建一个项目 3. 打开git 运行以下命令: cd /d/workspace (切换目录到 d盘的workspace) git init ( ...
- [CTCI] 下一个较大元素
下一个较大元素 题目描述 现在我们有一个int数组,请你找出数组中每个元素的下一个比它大的元素. 给定一个int数组A及数组的大小n,请返回一个int数组,代表每个元素比他大的下一个元素,若不存在则为 ...
- tensorflow省钱方案-ml-engine
google cloud有专门的ml-engine(machine learning engine)模块,可以直接用来跑tensorflow,不用像虚拟机一样开关机.只需要根据需要指定配置就行.收费分 ...