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显示,把菜单的值显示出来。

到这里,功能是已经完成了,但是呢,是不是就能成功运行呢?

问题还是有的!!发布的时候,出现下面的问题。

1>C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(348,2): error XA5208: Download failed. Please download https://dl-ssl.google.com/android/repository/android_m2repository_r29.zip and put it to the C:\Users\Catcher\AppData\Local\Xamarin\Android.Support.v4\23.3.0.0 directory.
 
1>C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(348,2): error XA5208: Reason: One or more errors occurred.
 
1>C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(348,2): error XA5207: Please install package: 'Xamarin.Android.Support.v4' available in SDK installer. Java library file C:\Users\Catcher\AppData\Local\Xamarin\Android.Support.v4\23.3.0.0\embedded\classes.jar doesn't exist.
 
1>C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(348,2): error XA5208: Download failed. Please download https://dl-ssl.google.com/android/repository/android_m2repository_r29.zip and put it to the C:\Users\Catcher\AppData\Local\Xamarin\Android.Support.v4\23.3.0.0 directory.
 
1>C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(348,2): error XA5208: Reason: One or more errors occurred.
 
1>C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(348,2): error XA5207: Please install package: 'Xamarin.Android.Support.v4' available in SDK installer. Java library file C:\Users\Catcher\AppData\Local\Xamarin\Android.Support.v4\23.3.0.0\embedded\libs/internal_impl-23.3.0.jar doesn't exist.

下面给出解决方案。

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可以去看看

http://www.xamarin.xyz/

Xamarin.Android之简单的抽屉布局的更多相关文章

  1. Android笔记:DrawerLayout抽屉布局的使用

    DrawerLayout(抽屉布局),在各种app中经常出现,比如csdn.. 如下示,只要从屏幕侧边滑一下,或者点击左上角的图标,抽屉就会出来. DrawerLayout要点: 1.使用Drawer ...

  2. android 开发 简单的页面布局

    package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view ...

  3. Xamarin.Android之引导页的简单制作

    0x01 前言 对于现在大部分的APP,第一次打开刚安装或更新安装的APP都会有几个引导界面,通常这几个引导页是告诉用户 APP有些什么功能或者修改了什么bug.新增了什么功能等等等. 下面就用Xam ...

  4. Xamarin.Android 引导页

    http://blog.csdn.net/qq1326702940/article/details/78665588 https://www.cnblogs.com/catcher1994/p/555 ...

  5. Xamarin android CardView的使用详解

    android 5.0新增加的一个控件CardView,在support v7兼容包中,意思就是卡片View,虽然可以设置阴影,圆角等等样式,但是我们也可以自己写出来,谷歌工程师之所以出这个,肯定是帮 ...

  6. Xamarin.Android和UWP之MVVM的简单使用(二)

    0x01 前言 前面一篇,Xamarin.Android和UWP之MVVM的简单使用(一),主要讲了MvvmLight的简单使用 这篇主要讲讲MvvmCross的简单使用,例子的话,还是和上篇的一样. ...

  7. [置顶] xamarin android 布局尺寸了解

    为了使UI界面在不同大小的移动端显示器上能够正常显示,大家可能都知道使用sp作为字体大小的单位,dp作为其他元素长度的单位. 前几天看了一篇文章关于 App设计规范的,文章用心写的非常好,这里是链接  ...

  8. xamarin android布局

    xamarin android布局练习(1) xamarin android布局练习,基础非常重要,首先要学习的就是android的布局练习,xamarin也一样,做了几个xamarin androi ...

  9. 基于Xamarin Android实现的简单的浏览器

    最近做了一个Android浏览器,当然功能比较简单,主要实现了自己想要的一些功能……现在有好多浏览器为什么还要自己写?当你使用的时候总有那么一些地方不如意,于是就想自己写一个. 开发环境:Xamari ...

随机推荐

  1. 安卓中級教程(11):深入研究餓了麼的各個java檔運作關係(1)

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

  2. Windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes

    Windows mysql提示:1045 access denied for user 'root'@'localhost' using password yes http://blog.csdn.n ...

  3. C#动态编译引擎-CS-Script

    什么是CS-Script? CS-Script是一种以CLR(公共语言运行库)为基础的脚本系统,它使用ECMA标准的C#作为编程语言,它面向微软的CLR运行库(.net 2.0/3.0/3.5/4.0 ...

  4. [PHP源码阅读]strlen函数

    文章来自:http://www.hoohack.me/2016/02/22/phps-source-analytics-strlen 我在github有对PHP源码更详细的注解.感兴趣的可以围观一下, ...

  5. 我只是想开个饭店—— JavaIO模型的演变

    Java的IO...真的是我所见过的高级语言中.最最复杂的... 看着这个图我也是醉了. 但是不知不觉间,java的IO已经更新到了NIO.2了,IO库早已经不止是这个样子了,那么这个过程中,它们经历 ...

  6. imagepool前端图片加载管理器(JavaScript图片连接池)

    前言 imagepool是一款管理图片加载的JS工具,通过imagepool可以控制图片并发加载个数. 对于图片加载,最原始的方式就是直接写个img标签,比如:<img src="图片 ...

  7. ASP.NET MVC 5 - 给数据模型添加校验器

    在本节中将会给Movie模型添加验证逻辑.并且确保这些验证规则在用户创建或编辑电影时被执行. 拒绝重复 DRY ASP.NET MVC 的核心设计信条之一是DRY: "不要重复自己(DRY ...

  8. nodejs 使用fs实现多级联动

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0gAAAEdCAIAAAC5WdDhAAAgAElEQVR4nO3da3Mc153f8X4feq5lFR

  9. Angular.js Services

    Angular带来了很多类型的services.每个都会它自己不同的使用场景.我们将在本节来阐述. 首先我们必须记在心里的是所有的services都是singleton(单例)的,这也是我们所希望得到 ...

  10. 解决Oracle SQL Developer无法连接远程服务器的问题

    在使用Oracle SQL Developer连接远程服务器的时候,出现如下的错误 在服务器本地是可以正常连接的.这个让人想起来,跟SQL Server的一些设计有些类似,服务器估计默认只在本地监听, ...