xamarin android Fragment实现底部导航栏

小编 2026-07-03 阅读:1124 评论:0
前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragmen...

前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment   Xamarin Android Fragment的两种加载方式。下面的这个例子介绍xamarin android fragment实现简单的底部导航栏。

效果图和项目结构

效果图:
xamarin android Fragment实现底部导航栏
项目结构:
xamarin android Fragment实现底部导航栏

实现步骤

主要的流程就是:点击不同的菜单加载对应的fragment出来,同时菜单icon切换和菜单文字颜色也响应变化,是否选中是通过selected来判断的。我们需要写以下几个资源文件,文字颜色的变化,菜单图片的变化。

step1:底部菜单资源文件

文字颜色变化资源: tab__menu_text.xml 
<?xml version="1.0" encoding="utf-8" ?><selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:color="@color/color_primary" android:state_selected="true"></item>  <item android:color="@color/color_808080"></item></selector>
菜单图片的变化资源:tab_menu_chat.xmltab_menu_more.xmltab_menu_contracts.xml 
<?xml version="1.0" encoding="utf-8" ?><selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:drawable="@drawable/more_selected" android:state_selected="true"></item>  <item android:drawable="@drawable/more"></item></selector>
三个都是一样的样式。

step2:MainActivity布局文件 Main.axml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/relativelayout1"    android:background="@color/color_primary"     android:fitsSystemWindows="true">    <RelativeLayout        android:id="@+id/ly_top_bar"        android:layout_width="match_parent"        android:layout_height="48dp"        android:background="@color/color_primary">        <TextView            android:id="@+id/txt_topbar"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_centerInParent="true"            android:gravity="center"            android:textSize="18sp"            android:textColor="@color/color_white"            android:text="信息" />        <View            android:layout_width="match_parent"            android:layout_height="2px"            android:background="@color/div_white"            android:layout_alignParentBottom="true" />    </RelativeLayout>    <LinearLayout        android:id="@+id/ly_tab_bar"        android:layout_width="match_parent"        android:layout_height="58dp"        android:layout_alignParentBottom="true"        android:background="@color/bg_white"        android:orientation="horizontal">        <TextView            android:id="@+id/txt_chat"            style="@style/tabText"            android:drawableTop="@drawable/tab_menu_chat"            android:text="我的"/>      <TextView       android:id="@+id/txt_more"       style="@style/tabText"       android:drawableTop="@drawable/tab_menu_more"       android:text="更多"/>      <TextView       android:id="@+id/txt_contacts"       style="@style/tabText"       android:drawableTop="@drawable/tab_menu_contacts"       android:text="联系人"/>    </LinearLayout>    <View        android:id="@+id/div_tab_bar"        android:layout_width="match_parent"        android:layout_height="2px"        android:background="@color/div_white"        android:layout_above="@id/ly_tab_bar" />    <FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@id/ly_top_bar"        android:layout_above="@id/div_tab_bar"        android:id="@+id/ly_content" /></RelativeLayout>

关于布局采用的相对布局分为三个部分:头部标题、中间Fragment的位置、底部导航栏。关于根布局文件中fitsSystemWindows属性是为了配合透明状态栏使用的,有兴趣的可以看看前几天的写的那篇文章。底部导航栏文字很多属性都是一模一样的,所以提出来,写一个style。使用widget属性让其各占1/3。文字样式tabText如下:
<?xml version="1.0" encoding="utf-8" ?><selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:color="@color/color_primary" android:state_selected="true"></item>  <item android:color="@color/color_808080"></item></selector>

step3:Fragment布局文件继承Fragment的类MyFragment

fg_content.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/bg_white">    <TextView        android:id="@+id/txt_content"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gravity="center"        android:text="呵呵"        android:textColor="@color/color_primary"        android:textSize="20sp" /></LinearLayout>

MyFragment.cs
    public class MyFragment : Fragment    {        private string content { get; set; }        public MyFragment(string  content)        {            this.content = content;        }        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)        {            View view = inflater.Inflate(Resource.Layout.fg_content, container, false);            TextView txt_content = (TextView)view.FindViewById(Resource.Id.txt_content);            txt_content.Text = content;            return view;        }    }

step3:MainActivity.cs 

    [Activity(Label = "FragmentDemo", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.Light.NoTitleBar")]    public class MainActivity : Activity    {        private TextView txt_chat;        private TextView txt_contacts;        private TextView txt_more;        private FrameLayout ly_content;        private MyFragment fg1, fg2, fg3;        private FragmentManager fManager;        protected override void OnCreate(Bundle bundle)        {            base.OnCreate(bundle);            SetContentView(Resource.Layout.Main);            ly_content = (FrameLayout)FindViewById(Resource.Id.ly_content);            MyFragment fg = new MyFragment("第一个fragment");            txt_chat = (TextView)FindViewById(Resource.Id.txt_chat);            txt_contacts = (TextView)FindViewById(Resource.Id.txt_contacts);            txt_more = (TextView)FindViewById(Resource.Id.txt_more);            bindViews();            txt_chat.PerformClick();        }        //ui组件初始化与事件绑定        private void bindViews()        {                      txt_chat.Click += (s, e) => { onClick(txt_chat); };            txt_contacts.Click += delegate { onClick(txt_contacts); };            txt_more.Click += delegate { onClick(txt_more); };        }        //隐藏所有Fragment        private void hideAllFragment(FragmentTransaction fragmentTransaction)        {            if (fg1 != null) fragmentTransaction.Hide(fg1);            if (fg2 != null) fragmentTransaction.Hide(fg2);            if (fg3 != null) fragmentTransaction.Hide(fg3);        }        //重置所有文本的选中状态        private void setSelected()        {            txt_chat.Selected =false;            txt_contacts.Selected = false;            txt_more.Selected = false;        }        //单击事件        public void onClick(View v)        {                 FragmentTransaction fTransaction = FragmentManager.BeginTransaction();                hideAllFragment(fTransaction);                switch (v.Id)                {                    case Resource.Id.txt_chat:                        setSelected();                        txt_chat.Selected = true;                        if (fg1 == null)                        {                            fg1 = new MyFragment("聊天Fragment");                            fTransaction.Add(Resource.Id.ly_content, fg1);                        }                        else{fTransaction.Show(fg1);}break;                    case Resource.Id.txt_contacts:                        setSelected();                        txt_contacts.Selected = true;                        if (fg2 == null)                        {                            fg2 = new MyFragment("联系人Fragment");                            fTransaction.Add(Resource.Id.ly_content, fg2);                        }                        else{fTransaction.Show(fg2);}                        break;                    case Resource.Id.txt_more:                        setSelected();                        txt_more.Selected = true;                        if (fg3 == null)                        {                            fg3 = new MyFragment("MoreFragment");                            fTransaction.Add(Resource.Id.ly_content, fg3);                        }else{fTransaction.Show(fg3);}break;                }                fTransaction.Commit();            }        }
关于继承的主题使用的android自带的主题Theme.Light.NoTitle,当然你也可以引入v7兼容包,继承AppcompatActivity,使用兼容包主题

step4:沉浸式状态栏

这个随手也实现一下吧,挺简单的。Main.axml中根布局中已经设置了属性fitsSystemWindows,兼容android4.4 和安定肉ID5.* ,我们在用代码设置状态栏透明就可以。有关的介绍可以参考  Xamarin android沉浸式状态栏
            if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Kitkat)            {                //透明状态栏                  Window.AddFlags(WindowManagerFlags.TranslucentStatus);                //透明导航栏                  Window.AddFlags(WindowManagerFlags.TranslucentNavigation);            }



代码下载:http://download.csdn.net/detail/kebi007/9820839

作者:张林

标题:xamarin android Fragment实现底部导航栏 原文地址:http://blog.csdn.net/kebi007/article/details/70307509

转载随意注明出处

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表