Android UI—仿微信底部导航栏布局

156 篇文章 1 订阅
117 篇文章 1 订阅

声明:原文转自博客作者FlyElephant,如有侵权,请联系删除
原文链接:http://www.cnblogs.com/xiaofeixiang
https://www.cnblogs.com/xiaofeixiang/p/4156732.html

正文

现在App基本的标配除了侧滑菜单,还有一个就是底部导航栏,常见的聊天工具QQ,微信,购物App都有底部导航栏,用户可以随便切换看不同的内容,说是情怀也好,用户体验也罢。我们开发的主要的还是讲的是如何如何实现其功能,网上实现的方式无外乎两种,一种是使用tabhost进行切换,一种是直接使用Fragment进行切换,底部导航栏的布局有的使用的是线性布局,有的是使用的RadioGroup,本文中是使用fragment+RadioGroup是实现的,看正文吧:

基础布局

其中主要低 底部导航栏,其他都没有什么,上面是一个Fragment自己替换一下即可

activity_main.xml中的布局文件,由于样式比较多可以单独的放在style中的,鉴于方便查看,直接放在布局文件中,activity_main中的代码:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.googlebottomfragment.MainActivity" >
<FrameLayout
    android:id="@+id/main_content"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

<RadioGroup
    android:id="@+id/tab_menu"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/mmfooter_bg"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/rbChat"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:background="@drawable/tab_selector_checked_bg"
        android:button="@null"
        android:checked="true"
        android:drawableTop="@drawable/tab_selector_weixing"
        android:gravity="center_horizontal|bottom"
        android:paddingTop="2dp"
        android:text="微信"
        android:textColor="@color/tab_selector_tv_color" />

    <RadioButton
        android:id="@+id/rbAddress"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:background="@drawable/tab_selector_checked_bg"
        android:button="@null"
        android:drawableTop="@drawable/tab_selector_tongxunlu"
        android:gravity="center_horizontal|bottom"
        android:paddingTop="2dp"
        android:text="通讯录"
        android:textColor="@color/tab_selector_tv_color" />

    <RadioButton
        android:id="@+id/rbFind"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:background="@drawable/tab_selector_checked_bg"
        android:button="@null"
        android:drawableTop="@drawable/tab_selector_faxian"
        android:gravity="center_horizontal|bottom"
        android:paddingTop="2dp"
        android:text="发现"
        android:textColor="@color/tab_selector_tv_color" />

    <RadioButton
        android:id="@+id/rbMe"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:background="@drawable/tab_selector_checked_bg"
        android:button="@null"
        android:drawableTop="@drawable/tab_selector_wo"
        android:gravity="center_horizontal|bottom"
        android:paddingTop="2dp"
        android:text="我"
        android:textColor="@color/tab_selector_tv_color" />
</RadioGroup></LinearLayout>

看下新建的布局和资源文件:

其中tab_selector_tv_color.xml主要是用于控制切换的时候显示下面字体的颜色:

`<?xml version=``"1.0"` `encoding=``"utf-8"``?>`

`<selector xmlns:android=``"http://schemas.android.com/apk/res/android"``>`

`<item android:state_checked=``"true"` `android:color=``"@android:color/white"``/>`

`<item android:state_checked=``"false"` `android:color=``"@android:color/darker_gray"``/>`

`<item android:color=``"@android:color/darker_gray"``/>`

`</selector>`

其中tab_selector_checked_bg.xml布局文件选中的时候每个RadioButtton的背景颜色:

`<?xml version=``"1.0"` `encoding=``"utf-8"``?>`

`<selector xmlns:android=``"http://schemas.android.com/apk/res/android"` `>`

`<item`

`android:state_checked=``"true"`

`android:drawable=``"@drawable/tab_bg_halo"``/>`

`</selector>`

其中tab_selector_weixing.xml主要是点击的时候显示不同的图片,一个是绿色的,一个是白色:

`<?xml version=``"1.0"` `encoding=``"utf-8"``?>`

`<selector xmlns:android=``"http://schemas.android.com/apk/res/android"` `>`

`<item android:state_checked=``"false"` `android:drawable=``"@drawable/tab_weixin_normal"``></item>`

`<item android:state_checked=``"true"` `android:drawable=``"@drawable/tab_weixin_pressed"``></item>`

`</selector>`

其中需要切换的chat.xml,address.xml,find.xml,me.xml都是一样的,其中chat.xml代码如下:

`<?xml version=``"1.0"` `encoding=``"utf-8"``?>`

`<LinearLayout xmlns:android=``"http://schemas.android.com/apk/res/android"`

`android:layout_width=``"match_parent"`

`android:layout_height=``"match_parent"`

`android:orientation=``"vertical"` `>`

`<TextView`

`android:layout_height=``"wrap_content"`

`android:layout_width=``"wrap_content"`

`android:text=``"微信"`

`android:textSize=``"20sp"`

`/>`

`<TextView`

`android:layout_height=``"wrap_content"`

`android:layout_width=``"wrap_content"`

`android:text=``"http://www.cnblogs.com/xiaofeixiang"`

`android:textSize=``"15sp"`

`/>`

`</LinearLayout>`

实现Demo

MainActivity.java中的代码,主要的就是设置一下OnCheckedChangeListener,注意MainActivity中需要继承FragmentActivity:

`public` `void` `initView() {`

`chat = ``new` `FragmentChat();`

`getSupportFragmentManager().beginTransaction().replace(R.id.main_content, chat).commit();`

`myTabRg = (RadioGroup) findViewById(R.id.tab_menu);`

`myTabRg.setOnCheckedChangeListener(``new` `OnCheckedChangeListener() {`

`@Override`

`public` `void` `onCheckedChanged(RadioGroup group, ``int` `checkedId) {`

`// TODO Auto-generated method stub`

`switch` `(checkedId) {`

`case` `R.id.rbChat:`

`chat = ``new` `FragmentChat();`

`getSupportFragmentManager().beginTransaction().replace(R.id.main_content, chat)`

`.commit();`

`break``;`

`case` `R.id.rbAddress:`

`if` `(address==``null``) {`

`address =``new` `FragmentAddress();`

`}`

`Log.i(``"MyFragment"``, ``"FragmentAddress"``);`

`getSupportFragmentManager().beginTransaction().replace(R.id.main_content, address).commit();`

`break``;`

`case` `R.id.rbFind:`

`find = ``new` `FragmentFind();`

`getSupportFragmentManager().beginTransaction().replace(R.id.main_content, find)`

`.commit();`

`break``;`

`case` `R.id.rbMe:`

`me = ``new` `FragmentMe();`

`getSupportFragmentManager().beginTransaction().replace(R.id.main_content, me)`

`.commit();`

`break``;`

`default``:`

`break``;`

`}`

`}`

`});`

FragmentChat中的代码,其余的三个FragmentAddress,FragmentFind,FragmentMe类似,就不贴代码了,主要是继承Fragment 即可:

`public` `class` `FragmentChat ``extends` `Fragment {`

`@Override`

`public` `void` `onCreate(Bundle savedInstanceState) {`

`// TODO Auto-generated method stub`

`super``.onCreate(savedInstanceState);`

`}`

`@Override`

`public` `View onCreateView(LayoutInflater inflater,`

`@Nullable` `ViewGroup container, ``@Nullable` `Bundle savedInstanceState) {`

`// TODO Auto-generated method stub`

`return` `inflater.inflate(R.layout.chat, ``null``);`

`}`

`}`

最后看张通讯录的截图吧:

最后

一点题外话:

之前答应大家整理的资料都整理好了

点击:

Android学习PDF+架构视频+面试文档+源码笔记》(不是本人发放)

 

来获取学习资料提升自己去挑战一下

当程序员容易,当一个优秀的程序员是需要不断学习的,从初级程序员到高级程序员,从初级架构师到资深架构师,或者走向管理,从技术经理到技术总监,每个阶段都需要掌握不同的能力。早早确定自己的职业方向,才能在工作和能力提升中甩开同龄人。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值