Xamarin.Android之简单的抽屉布局
0x01 前言
相信对于用过Android版QQ的,应该都不会陌生它那个向右滑动的菜单(虽说我用的是Lumia)
今天就用Xamarin.Android实现个比较简单的抽屉布局。下面直接进正题。
0x02 做个简单的抽屉布局
新建个android项目
通过NuGet安装个Xamarin.Android.Support.v4

其实呢,官网那里还用很多组件可用拿来尝试一下的。
https://components.xamarin.com/
然后修改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.support.v4.widget.DrawerLayout
android:id="@+id/mDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/holo_blue_light"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
这里用了相对布局,更重要的是android.support.v4.widget.DrawerLayout
同时新建一个fragmentcontent.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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAlignment="center"
android:textSize="30dp"
android:id="@+id/txtName" />
</LinearLayout>
内容比较简单,就是显示相应菜单的文本。
然后,修改MainActivity
using Android.App;
using Android.OS;
using Android.Support.V4.Widget;
using Android.Widget;
namespace DrawerLayoutDemo
{
[Activity(Label = "DrawerLayoutDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private string[] _menu;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle); SetContentView(Resource.Layout.Main);
//the menu
_menu = new string[] { "C#", "Python", "Xamarin" };
//listview
var listView = FindViewById<ListView>(Resource.Id.left_drawer);
//adapter
listView.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, _menu);
//drawerlayout
var drawerLayout = FindViewById<DrawerLayout>(Resource.Id.mDrawerLayout);
//click event
listView.ItemClick += ItemClick;
}
/// <summary>
/// item click event of the listview
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//fragment
Fragment fragment = new FragmentContent(_menu[e.Position]);
//show
var fm = FragmentManager.BeginTransaction().Replace(Resource.Id.content_frame, fragment).Commit();
}
}
}
MainActivity的话主要是处理ListView的绑定以及点击事件。
新建一个FragmentContent
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
namespace DrawerLayoutDemo
{
public class FragmentContent : Fragment
{
private string _text;
public FragmentContent(string text)
{
_text = text;
}
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
} public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//get the view
View view = inflater.Inflate(Resource.Layout.fragmentcontent, null);
var txt = view.FindViewById<TextView>(Resource.Id.txtName);
//set the text of the textview
txt.Text = "I Love " + _text;
return view;
}
}
}
Fragment的话,就是显示把fragmentcontent.axml显示,把菜单的值显示出来。
到这里,功能是已经完成了,但是呢,是不是就能成功运行呢?
问题还是有的!!发布的时候,出现下面的问题。
下面给出解决方案。
0x03 出错处理方案
从错误我们能看出缺少东西了。
https://dl-ssl.google.com/android/repository/android_m2repository_r29.zip
其实这个文件是可以直接下载的,不用翻墙。但是在生成或是发布的时候下载会出错。
在C:\Users\Catcher\AppData\Local\Xamarin\zips下面(这个是下载之后所在的目录)

这个zip文件一直是处于无效的状态。所以只能单独下载上面的那个文件,然后把文件放在
zips那个目录下面,同时改为这个名字,即可。
然后再生成就不会出现问题了。
同时它会在C:\Users\Catcher\AppData\Local\Xamarin\Android.Support.v4\23.3.0.0目录下生成下面两个文件夹

0x04 效果图

当然,这个demo简单到不行,想弄好看点的话就自己自定义listview的样式
文字旁边个图标之类的。。然后写个好看的布局。。
最后推荐马跃大哥的博客,学Xamarin.Android可以去看看
Xamarin.Android之简单的抽屉布局的更多相关文章
- Android笔记:DrawerLayout抽屉布局的使用
DrawerLayout(抽屉布局),在各种app中经常出现,比如csdn.. 如下示,只要从屏幕侧边滑一下,或者点击左上角的图标,抽屉就会出来. DrawerLayout要点: 1.使用Drawer ...
- android 开发 简单的页面布局
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view ...
- Xamarin.Android之引导页的简单制作
0x01 前言 对于现在大部分的APP,第一次打开刚安装或更新安装的APP都会有几个引导界面,通常这几个引导页是告诉用户 APP有些什么功能或者修改了什么bug.新增了什么功能等等等. 下面就用Xam ...
- Xamarin.Android 引导页
http://blog.csdn.net/qq1326702940/article/details/78665588 https://www.cnblogs.com/catcher1994/p/555 ...
- Xamarin android CardView的使用详解
android 5.0新增加的一个控件CardView,在support v7兼容包中,意思就是卡片View,虽然可以设置阴影,圆角等等样式,但是我们也可以自己写出来,谷歌工程师之所以出这个,肯定是帮 ...
- Xamarin.Android和UWP之MVVM的简单使用(二)
0x01 前言 前面一篇,Xamarin.Android和UWP之MVVM的简单使用(一),主要讲了MvvmLight的简单使用 这篇主要讲讲MvvmCross的简单使用,例子的话,还是和上篇的一样. ...
- [置顶]
xamarin android 布局尺寸了解
为了使UI界面在不同大小的移动端显示器上能够正常显示,大家可能都知道使用sp作为字体大小的单位,dp作为其他元素长度的单位. 前几天看了一篇文章关于 App设计规范的,文章用心写的非常好,这里是链接 ...
- xamarin android布局
xamarin android布局练习(1) xamarin android布局练习,基础非常重要,首先要学习的就是android的布局练习,xamarin也一样,做了几个xamarin androi ...
- 基于Xamarin Android实现的简单的浏览器
最近做了一个Android浏览器,当然功能比较简单,主要实现了自己想要的一些功能……现在有好多浏览器为什么还要自己写?当你使用的时候总有那么一些地方不如意,于是就想自己写一个. 开发环境:Xamari ...
随机推荐
- CSS 使用母版页的内容页如何调用css和javascript
方案一: 把所有的css样式和javascript函数放到母版页的<head></head>中,我觉得这样做的弊端就是导致母版页的<head></head&g ...
- Mac读取Andriod屏幕截图
int main(int argc, const char * argv[]) { // insert code here... string str3 = "/Users/Ethan/Do ...
- mac显示和隐藏文件
封装了一下显示和隐藏的脚本,方便mac上的文件隐藏和显示 if [ `defaults read com.apple.finder AppleShowAllFiles` = "1" ...
- CYQ.Data 快速开发EasyUI
EasyUI: 前端UI框架之一, 相对ExtJs来说,算是小了,这两天,抽空看了下EasyUI的相关知识,基本上可以和大伙分享一下: 官网: http://www.jeasyui.com/ 学习的话 ...
- mysql字符串处理例子
项目中用到的,要判断表中某个字段的某几位,若为某个值则替换,用到了几个典型的字符串操作,记录备注实现方案如下: 备注:如果替代字符串是唯一的话,可以用replace,这里用的是concat拼接. DE ...
- [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序
[.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序 本节导读: 本节主要说明使用异步进行程序设计的优缺点及如何通过异步编程. 使用 ...
- 实现Div拖拽
直观的理解div拖拽:当鼠标对着可拖拽部分按住后并拖动,div会跟着鼠标一起运动,并且其运动空间限制在浏览器内部,当放开鼠标时,则div停止运动. 实现div拖拽需要三个重要的事件: (1)onmou ...
- 使用C#给Linux写Shell脚本(下篇)
在上篇的<使用C#给Linux写Shell脚本>结尾中,我们留下了一个关于C#如何调用BashShell的问题.在文章发布之后,我留意到有读者留言推荐使用“Pash”(一款类PowerSh ...
- 《App研发录》面世
古者富贵而名灭,不可胜记,唯倜傥非常之人称焉.故西伯拘而演<周易>,屈原放逐,乃赋<离骚>.文人雅士一次次的谱写着千古绝唱,而我亦不能免俗,也要附庸风雅,写一部前不见古人.后不 ...
- IDE:IDEA Commit Changes Dialog local changes refresh
IDEA提交代码,一直卡着不动,显示:Commit Changes Dialog local changes refresh 修改方法为: go to settings - version contr ...