Xamarin.Android 利用Fragment实现底部菜单
效果图:

第一步:添加引用
引用 Crosslight.Xamarin.Android.Support.v7.AppCompat 这个包。

第二步:绘制Main和Fragment界面
fg_home.axml
<?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="#FFFFFF">
<TextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="首页"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
fg_label.axml
<?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="#FFFFFF">
<TextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="贴签"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
fg_mine.axml
<?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="#FFFFFF">
<TextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="我的"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
fg_query.axml
<?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="#FFFFFF">
<TextView
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="查询"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/main_left" />
</LinearLayout>
main_left.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dl_left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0">
<!--主布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativelayout1"
android:fitsSystemWindows="true">
<RelativeLayout
android:id="@+id/ly_top_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:visibility="gone">
</RelativeLayout>
<LinearLayout
android:id="@+id/ly_tab_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#FFFFFF"
android:orientation="vertical"> <View
android:layout_width="match_parent"
android:layout_height="2px"
android:background="#cccccc" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<ImageView
android:id="@+id/iv_home"
android:layout_width="25.6dp"
android:layout_height="37.6dp"
android:src="@drawable/icon_home1"
android:layout_weight="1"/>
<ImageView
android:id="@+id/iv_query"
android:layout_width="25.6dp"
android:layout_height="37.6dp"
android:src="@drawable/icon_query1"
android:layout_weight="1"/>
<ImageView
android:id="@+id/iv_label"
android:layout_width="25.6dp"
android:layout_height="37.6dp"
android:src="@drawable/icon_label1"
android:layout_weight="1"/>
<ImageView
android:id="@+id/iv_mine"
android:layout_width="25.6dp"
android:layout_height="37.6dp"
android:src="@drawable/icon_mine1"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<View
android:id="@+id/div_tab_bar"
android:layout_width="match_parent"
android:layout_height="2px"
android:background="#FFFFFF"
android:layout_above="@id/ly_tab_bar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fy_home"
android:layout_below="@id/ly_top_bar"
android:layout_above="@id/div_tab_bar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fy_query"
android:layout_below="@id/ly_top_bar"
android:layout_above="@id/div_tab_bar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fy_label"
android:layout_below="@id/ly_top_bar"
android:layout_above="@id/div_tab_bar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fy_mine"
android:layout_below="@id/ly_top_bar"
android:layout_above="@id/div_tab_bar"/>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
第三步:在value文件下创建Style,并且自定义 BaseAppTheme 样式
<?xml version="1.0" encoding="utf-8" ?>
<resources> <color name="primary">#1e89e7</color>
<color name="primaryDark">#1976d2</color>
<color name="red">#ff0000</color>
<color name="white">#ffffff</color> <style name="BaseAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
<item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
</style> <style name="AppTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
<item name="color">@android:color/white</item>
</style>
</resources>
第四步:编写每个Fragment的后台,这里只写一个。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget; namespace BottomMuneDemo.Fragments
{
public class HomeFragment : Fragment
{
private string content { get; set; }
public HomeFragment(string content)
{
this.content = content;
} public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); // Create your fragment here
} public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.fg_home, container, false);
TextView txt_content = (TextView)view.FindViewById(Resource.Id.txt_content);
txt_content.Text = "首页"; return view;
}
}
}
第五步:在Main活动中进行设置。
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using BottomMuneDemo.Fragments;
using Android.Views; namespace BottomMuneDemo
{
[Activity(Label = "BottomMuneDemo", MainLauncher = true, Theme = "@style/BaseAppTheme")]
public class MainActivity : AppCompatActivity
{
private ImageView iv_home;
private ImageView iv_query;
private ImageView iv_label;
private ImageView iv_mine; private FrameLayout fy_home;
private FrameLayout fy_query;
private FrameLayout fy_label;
private FrameLayout fy_mine; HomeFragment fg1;
QueryFragment fg2;
LabelFragment fg3;
MineFragment fg4; protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main); fy_home = (FrameLayout)FindViewById(Resource.Id.fy_home);
fy_query = (FrameLayout)FindViewById(Resource.Id.fy_query);
fy_label = (FrameLayout)FindViewById(Resource.Id.fy_label);
fy_mine = (FrameLayout)FindViewById(Resource.Id.fy_mine); iv_home = (ImageView)FindViewById(Resource.Id.iv_home);
iv_query = (ImageView)FindViewById(Resource.Id.iv_query);
iv_label = (ImageView)FindViewById(Resource.Id.iv_label);
iv_mine = (ImageView)FindViewById(Resource.Id.iv_mine); bindViews();
iv_home.PerformClick(); } #region 底部菜单选项卡 //ui组件初始化与事件绑定
private void bindViews()
{ iv_home.Click += (s, e) => { onClick(iv_home); };
iv_query.Click += delegate { onClick(iv_query); };
iv_label.Click += delegate { onClick(iv_label); };
iv_mine.Click += delegate { onClick(iv_mine); };
}
//隐藏所有Fragment
private void hideAllFragment(FragmentTransaction fragmentTransaction)
{
if (fg1 != null) fragmentTransaction.Hide(fg1);
if (fg2 != null) fragmentTransaction.Hide(fg2);
if (fg3 != null) fragmentTransaction.Hide(fg3);
if (fg4 != null) fragmentTransaction.Hide(fg4); iv_home.SetImageResource(Resource.Drawable.icon_home1);
iv_query.SetImageResource(Resource.Drawable.icon_query1);
iv_label.SetImageResource(Resource.Drawable.icon_label1);
iv_mine.SetImageResource(Resource.Drawable.icon_mine1);
}
//重置所有文本的选中状态
private void setSelected()
{
iv_home.Selected = false;
iv_query.Selected = false;
iv_label.Selected = false;
iv_mine.Selected = false;
}
//单击事件
public void onClick(View v)
{
FragmentTransaction fTransaction = FragmentManager.BeginTransaction();
hideAllFragment(fTransaction);
switch (v.Id)
{
case Resource.Id.iv_home:
setSelected();
iv_home.Selected = true;
iv_home.SetImageResource(Resource.Drawable.icon_home2);
if (fg1 == null)
{
fg1 = new HomeFragment("首页");
fTransaction.Add(Resource.Id.fy_home, fg1);
}
else { fTransaction.Show(fg1); }
break; case Resource.Id.iv_query:
setSelected();
iv_query.Selected = true;
iv_query.SetImageResource(Resource.Drawable.icon_query2);
if (fg2 == null)
{
fg2 = new QueryFragment("查询");
fTransaction.Add(Resource.Id.fy_query, fg2);
}
else { fTransaction.Show(fg2); }
break; case Resource.Id.iv_label:
setSelected();
iv_label.Selected = true;
iv_label.SetImageResource(Resource.Drawable.icon_label2);
if (fg3 == null)
{
fg3 = new LabelFragment("贴签");
fTransaction.Add(Resource.Id.fy_label, fg3);
}
else { fTransaction.Show(fg3); }
break; case Resource.Id.iv_mine:
setSelected();
iv_mine.Selected = true;
iv_mine.SetImageResource(Resource.Drawable.icon_mine2);
if (fg4 == null)
{
fg4 = new MineFragment("我的");
fTransaction.Add(Resource.Id.fy_mine, fg4);
}
else { fTransaction.Show(fg4); }
break;
}
fTransaction.Commit();
}
#endregion
}
}
到这里就结束了,亲测代码有效,如有问题请留言。
Xamarin.Android 利用Fragment实现底部菜单的更多相关文章
- Android应用主界面底部菜单实现
介绍 现在绝大多数主流的应用主界面,都会包含一个底部菜单,就拿腾讯的QQ与微信来说,看起来是这样的 <---我是底部菜单 原理 在很久以前,可以通过TabActivity实现相关功能,自从Fr ...
- Xamarin.Android之Fragment Walkthrough
利用Fragment设计能够兼容不同屏幕的应用 这里我们先围观下最后的成果图,给读者打打气: 普通手机上显示的结果: 在平板上显示的结果: 笔者要郑重声明下,虽然看似是两种不同的显示效果,但是同一个应 ...
- Android编程: fragment组件、菜单和Intent组件
学习内容:fragment组件.菜单和Intent组件 ====fragment组件====1.fragment是一种自我容纳,模块化的,嵌入在一个Activity里面的视图组件 可以在运行时动 ...
- Android自定义控件系列(四)—底部菜单(下)
转载请注明出处:http://www.cnblogs.com/landptf/p/6290862.html 在app中经常会用到底部菜单的控件,每次都需要写好多代码,今天我们用到了前几篇博客里的控件来 ...
- Activity内切换fragment实现底部菜单切换遇到的坑
1.一般说来,app底部导航都会设计为5个菜单,可以使用textView,也可使用radioButton,这里我选择用radioButton,给radioButton直接设置selector就可以实现 ...
- 利用TabWidget实现底部菜单
TabWidget类似于通话记录的界面,通过切换多个标签从而显示出多个不同内容,能够展示内容丰富的页面信息,而且彼此之间不会干扰,有利于展示.下面,通过一个例子来学习用法 首先用一个类来继承TabAc ...
- [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单
Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...
- Xamarin.Android开发实践(十六)
Xamarin.Android之Fragment Walkthrough 利用Fragment设计能够兼容不同屏幕的应用 这里我们先围观下最后的成果图,给读者打打气: 普通手机上显示的结果: 在平板上 ...
- [置顶]
xamarin android Fragment实现底部导航栏
前段时间写了篇关于Fragment的文章,介绍了基础的概念,用静态和动态的方式加载Fragment Xamarin Android Fragment的两种加载方式.下面的这个例子介绍xamarin ...
随机推荐
- JAVA实训第二次作业
一维数组的创建和遍历. 声明并创建存放4个人考试成绩的一维数组,并使用for循环遍历数组并打印分数.要求: (1) 首先按"顺序"遍历,即打印顺序为:从第一个人到第四个人: (2) ...
- Altium Designer 16 问题解决
1:同一个工程中,不同原理图里的网络标号不能关联起来 解决---> 选择 工程->工程参数->网络识别符范围 -> GLOBAL 2:PCB中影藏显示相应Net的飞线 解 ...
- pip使用国内镜像安装各种库
1. 指定阿里云镜像, 安装requirements.txt中的所有 pip install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-h ...
- Spring-boot集成RabbitMQ踩过的坑
1.java.net.SocketException: socket closed 官方文档已经说明,新建user和guest的账户是没有远程登录的权限的 需要对登录所用账户授权 解决方法: rabb ...
- 计算机爱好者协会技术贴markdown第三期
之前都是给大家展示的对文本的基本操作,这一期我们要给大家展示的就是很高大上的东西了哦 看左边是不是超low,右边是不是瞬间高大上了!!! 一个简单的表格是这么创建的: 爱酱 | Value - ...
- 81、iOS本地推送与远程推送详解
一.简介 分为本地推送和远程推送2种.可以在应用没打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户石否同意,如果同意则正常使用:如果用户不同意则下次打开程序 ...
- .Net WebApi开发SharePoint出现System.IO.FileNotFoundException: 找不到位于的 Web 应用程序
System.IO.FileNotFoundException: 找不到位于 http://xxx/sites/xxxx 的 Web 应用程序.请确认正确键入了此 URL.如果此 URL 需要提供现有 ...
- update_or_create()
update_or_create(默认值=无,** kwargs)¶ 使用给定更新对象的便捷方法,kwargs必要时创建新对象.这defaults是用于更新对象的(字段,值)对的字典.值中的值defa ...
- Unity3D编辑器扩展(五)——常用特性(Attribute)以及Selection类
前面写了四篇关于编辑器的: Unity3D编辑器扩展(一)——定义自己的菜单按钮 Unity3D编辑器扩展(二)——定义自己的窗口 Unity3D编辑器扩展(三)——使用GUI绘制窗口 Unity3D ...
- .Net QQ互联教程
qq互联只需要备案即可申请,申请成功后可以到qq互联官网查看教程,本站开始想使用js的教程但是由于本站需要绑定本站的账号用js教程无法完成,所以使用原始的oauth2.0来完成. 申请qq互联接口 q ...