【效果】(这里下载的软件收费的试用有水印)

【推荐】这里推荐一个图标网http://iconfont.cn/。以上图标来自此图标网

【项目结构】

【步骤】

①创建布局文件,写底部导航栏

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <RelativeLayout
android:id="@+id/rl_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--这里设置权重weight为1, 下面不设置权重。-->
<!--意思是,剩余的位置全都是RelativeLayout的-->
</RelativeLayout> <TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#797878"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/item1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/item1_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:layout_margin="3dp"
android:scaleType="fitCenter"
android:src="@drawable/wxb"
android:padding="1dp"/>
<TextView
android:id="@+id/item1_tv"
android:text="女王"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/item2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/item2_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:layout_margin="3dp"
android:scaleType="fitCenter"
android:src="@drawable/meizhuang"
android:padding="4dp"/>
<TextView
android:id="@+id/item2_tv"
android:text="美妆"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:id="@+id/item3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/item3_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:layout_margin="3dp"
android:scaleType="fitCenter"
android:src="@drawable/fuzhuang"
android:padding="5dp"/>
<TextView
android:id="@+id/item3_tv"
android:text="衣帽"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:id="@+id/item4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/item4_iv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:layout_margin="3dp"
android:scaleType="fitCenter"
android:src="@drawable/xiebaopeishi"
android:padding="3dp"/>
<TextView
android:id="@+id/item4_tv"
android:text="鞋包"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

②定义Fragment

【提示】可以通过下图方式创建

 public class FragmentA extends Fragment {

     public FragmentA() {
// Required empty public constructor
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a, container, false);
}
}

对于生成的Fragment不用作修改,对应的布局中设置一个背景颜色便于观察。

③MainActivity代码的编写

 public class MainActivity extends AppCompatActivity implements View.OnClickListener{

     private FragmentManager fragmentManager;
private RelativeLayout rl_content;
private ImageView item1_iv,item2_iv,item3_iv,item4_iv;
private TextView item1_tv,item2_tv,item3_tv,item4_tv;
private LinearLayout item1,item2,item3,item4;
private ImageView[] ivs;
private TextView[] tvs; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initView(); fragmentManager = getSupportFragmentManager(); initListener();
} private void initListener() {
item1.setOnClickListener(this);
item2.setOnClickListener(this);
item3.setOnClickListener(this);
item4.setOnClickListener(this);
} private void initView() {
rl_content = (RelativeLayout) findViewById(R.id.rl_content);
item1_iv = (ImageView) findViewById(R.id.item1_iv);
item1_tv = (TextView) findViewById(R.id.item1_tv);
item1 = (LinearLayout) findViewById(R.id.item1);
item2_iv = (ImageView) findViewById(R.id.item2_iv);
item2_tv = (TextView) findViewById(R.id.item2_tv);
item2 = (LinearLayout) findViewById(R.id.item2);
item3_iv = (ImageView) findViewById(R.id.item3_iv);
item3_tv = (TextView) findViewById(R.id.item3_tv);
item3 = (LinearLayout) findViewById(R.id.item3);
item4_iv = (ImageView) findViewById(R.id.item4_iv);
item4_tv = (TextView) findViewById(R.id.item4_tv);
item4 = (LinearLayout) findViewById(R.id.item4);
ivs = new ImageView[]{item1_iv,item2_iv,item3_iv,item4_iv};
tvs = new TextView[]{item1_tv,item2_tv,item3_tv,item4_tv};
} @Override
public void onClick(View view) {
switch (view.getId()){
case R.id.item1: {
FragmentTransaction transaction = fragmentManager.beginTransaction();//创建一个事务
transaction.replace(R.id.rl_content,new FragmentA());
transaction.commit();//事务一定要提交,replace才会有效
setCheck(0);//自定义方法
break;
}
case R.id.item2: {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.rl_content,new FragmentB());
transaction.commit();
setCheck(1);
break;
}
case R.id.item3: {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.rl_content,new FragmentC());
transaction.commit();
setCheck(2);
break;
}
case R.id.item4: {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.rl_content,new FragmentD());
transaction.commit();
setCheck(3);
break;
}
default:break;
}
} public void setCheck(int itemId){
//这个方法设置底部导航栏选中时的效果
for (int i = 0; i < 4; i++) {
ivs[i].setColorFilter(Color.parseColor("#0f0f0f"));
tvs[i].setTextColor(Color.parseColor("#0f0f0f"));
}
ivs[itemId].setColorFilter(Color.GREEN);
tvs[itemId].setTextColor(Color.GREEN);
}
}

【提示】①这里的点击事件是通过Activity实现Onclick接口的方式

②getSupportFragmentManager()是v4包中的,兼容效果好,如果用getFragmentManager()可能会崩掉

③FragmentManager只需要获取一次,但是事务FragmentTransaction要重新开启。最后事务一定要提交commit。

④方法setCheck是为了设置导航的被选中效果。

底部导航栏实现一 Fragment-replace的更多相关文章

  1. AndroidStudio制作底部导航栏以及用Fragment实现切换功能

    前言 大家好,给大家带来AndroidStudio制作底部导航栏以及用Fragment实现切换功能的概述,希望你们喜欢 学习目标 AndroidStudio制作底部导航栏以及用Fragment实现切换 ...

  2. TextView+Fragment实现底部导航栏

    前言:项目第二版刚上线没多久,产品又对需求进行了大改动,以前用的是左滑菜单,现在又要换成底部导航栏,于是今天又苦逼加班了.花了几个小时实现了一个底部导航栏的demo,然后总结一下.写一篇博客.供自己以 ...

  3. 二、Fragment+RadioButton实现底部导航栏

    在App中经常看到这样的tab底部导航栏   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...

  4. Android学习笔记- Fragment实例 底部导航栏的实现

    1.要实现的效果图以及工程目录结构: 先看看效果图吧: 接着看看我们的工程的目录结构: 2.实现流程: Step 1:写下底部选项的一些资源文件 我们从图上可以看到,我们底部的每一项点击的时候都有不同 ...

  5. [置顶] xamarin android Fragment实现底部导航栏

    前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment  Xamarin Android Fragment的两种加载方式.下面的这个例子介绍xamarin ...

  6. Android商城开发系列(三)——使用Fragment+RadioButton实现商城底部导航栏

    在商城第一篇的开篇当中,我们看到商城的效果图里面有一个底部导航栏效果,如下图所示: 今天我们就来实现商城底部导航栏,最终效果图如下所示:   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使 ...

  7. Android_ViewPager+Fragment实现页面滑动和底部导航栏

    1.Xml中底部导航栏由一个RadioGroup组成,其上是ViewPager. <?xml version="1.0" encoding="utf-8" ...

  8. Android底部导航栏——FrameLayout + RadioGroup

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6285881.html Android底部导航栏有多种实现方式,本文详细介绍FrameLayout ...

  9. Android 底部导航栏实现一 Fragment-replace

    [效果](这里下载的软件收费的试用有水印) [推荐]这里推荐一个图标网http://iconfont.cn/.以上图标来自此图标网 [项目结构] [步骤] ①创建布局文件,写底部导航栏 <?xm ...

随机推荐

  1. HTML mate标签

    META标签分两大部分:HTTP标题信息(http-equiv)和页面描述信息(name). http-equiv http-equiv类似于HTTP的头部协议,它回应给浏览器一些有用的信息,以帮助正 ...

  2. [洛谷P2234][HNOI2002] 营业额统计 - Treap

    Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额. ...

  3. anguar使用指令写选项卡

    今天,我们来学习一下angular中怎么使用指令来实现两个选项卡的问题. 首先,要先引入jQuery文件与angularjs文件. <!DOCTYPE html><html lang ...

  4. Django:(博客系统)使用使用mysql数据&创建post/category/tag实体,并同步到数据中

    背景: 之前也读过一些关于django的一些书,看过别人写的一些博客系统.但是总有一种看别人的都会,但自己写不出来的感觉,于是为了加深对django的学习就开始动手学习了. 环境搭建: 环境:使用py ...

  5. vuex commit保存数据技巧

    vuex 单向数据流,推荐的commit 改变state数据,写起来非常繁琐,因为改数据可能要写很多commit函数. 依据我的理解,单向数据流主要是为了避免数据混乱,便于调试. 说白了,就是一个数据 ...

  6. Spring Cloud学习笔记-004

    高可用注册中心 在微服务架构这样的分布式环境中,需要充分考虑发生故障的情况,所以在生产环境中必须对各个组件进行高可用部署,对于微服务如此,对于服务注册中心也一样.如果一直使用单节点的服务注册中心,这在 ...

  7. Linux OpenGL 实践篇-4 坐标系统

    OpenGL中顶点经过顶点着色器后会变为标准设备坐标系.标准设备坐标系的各坐标的取值范围是[-1,1],超过这个范围的点将会被剔除.而这个变换的过程可描述为顶点在几个坐标系统的变换,这几个坐标系统为: ...

  8. [LeetCode] Valid Palindrome II 验证回文字符串之二

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  9. [LeetCode] Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  10. 不用第三方解码库取得图片宽高 附完整C++算法实现代码

    在特定的应用场景下,有时候我们只是想获取图片的宽高, 但不想通过解码图片才取得这个信息. 预先知道图片的宽高信息,进而提速图片加载,预处理等相关操作以提升体验. 在stackoverflow有一篇相关 ...