原文: Android(Xamarin)之旅(三)

前面两篇说到了Xamarin的安装和一些简单的控件,今天来说说一些对话框和提示信息,以及简单的布局元素。

一、对话框和提示信息

  一、对话框

      我们首先从简单的对话框开始。

      1、普通对话框

      在android里面,对话框用的是AlertDialog,这个呢,其实就和winform里面的MessageBox一样的。最简单的

AlertDialog.Builder ad_build = new AlertDialog.Builder(this)                              .SetTitle(Resource.String.warming)//标题(警告)                              .SetMessage(Resource.String.info)//获取本地string.xml定义的数据                              .SetNegativeButton("确定", this)                              .SetPositiveButton("取消", this)                              .SetNeutralButton("中间按钮", this)                              .SetIcon(Android.Resource.Drawable.StatSysWarning);                    ad_build.Show();

      其中,SetNeutralButton这里是设置的一个中间按钮,这个东西,可以有,也可以没有,代码可以直接添加到程序里面运行即可。在代码中,我们可以看到提示信息里面的, 获取本地strings.xml定义的数据。我们可以看下本地的xml数据。

<?xml version="1.0" encoding="utf-8"?><resources>  <string name="myapp">爺的APP</string>  <string name="info">您确定结束本次任务?</string>  <string name="yesorno">确定要退出吗?</string>  <string name="warming">警告</string>  <string name="jiazai">正在加载……</string>  <string name="Img_Info">您有一个未接电话</string>  <string name="MyToast">自定义提示信息</string></resources>

      在这里,不得不说一下,这里的SetIcon的问题,这里的Android.Resource.Drawable这个是系统自带的图片,可能我们有时候需要去访问本地自己的图片。

      在dialog显示gif图片 由于dialog不容易取到里面空间对象,推荐使用透明样式的activity,只需把该activity的样式设置为透明样式 即android:theme="@android:style/Theme.Translucent",同时在oncreat()方法的setcontenview()之前设置requestWindowFeature(Window.FEATURE_NO_TITLE);去掉标题. 播放gif图片 由于android控件不支持播放gif。推荐使用webview 里面放入html中含有img标签 src便是图片的地址 可以使网络地址 也可以是本地地址 然后webview加载该html即实现了播放

      2、单选对话框

      单选对话框,还用的是AlertDialog

 AlertDialog.Builder ad_build = new AlertDialog.Builder(this)                              .SetTitle(Resource.String.warming)//标题(警告)                              .SetSingleChoiceItems(new string[] { "中国", "日本", "韩国" }, 0, this)//自定义的单选数组                              .SetNegativeButton("确定", this)                              .SetPositiveButton("取消", this)
.SetIcon(Android.Resource.Drawable.StatSysWarning); ad_build.Show();

      在这里,任然可以添加中间按钮,直接在后面继续加点,加Set就可以,但是单选要实现IDialogInterfaceOnClickListener 接口,通过这个接口,我们可以获取到现在选择的到底是哪一个的值。其实,单选就是多了一个SetSingleChoiceItems这个参数,然后传值就可以。

        /// <summary>        /// 单选和普通对话框        /// </summary>        /// <param name="dialog"></param>        /// <param name="which"></param>        public void OnClick(IDialogInterface dialog, int which)        {            Toast.MakeText(this, which + "", ToastLength.Short).Show();        }

      这里的which就是选择的是哪一个的值,获取到值一般来说才是最重要的,我们才可以继续其他的事情。

      3、多选对话框

      多选对话框,还用的是AlertDialog

  AlertDialog.Builder ad_build = new AlertDialog.Builder(this)                             .SetTitle(Resource.String.warming)//标题(警告)                             .SetMultiChoiceItems(new string[] { "中国", "日本", "韩国" }, new bool[] { false, true, true }, this)//多选自定义数组                             .SetNegativeButton("确定", this)                             .SetPositiveButton("取消", this)                             .SetIcon(Android.Resource.Drawable.StatSysWarning);                    ad_build.Show();

      其中,多选框实现的IDialogInterfaceOnMultiChoiceClickListener 是这个接口。

     /// <summary>        /// 多选接口的实现        /// </summary>        /// <param name="dialog"></param>        /// <param name="which"></param>        /// <param name="isChecked"></param>        public void OnClick(IDialogInterface dialog, int which, bool isChecked)        {            Toast.MakeText(this, which.ToString() +"    "+ isChecked.ToString(), ToastLength.Short).Show();        }

      同样的,这里的which是在这个多选框中的唯一ID,后面的isChecked,是否选择,通过这些,我们就可以获取到很多信息了。

      4、正在加载对话框

      正在加载用的是ProgressDialog 这个方法,这个方法同样可以 Builder,但是和SetIcon一样,如果想采取自定义的图片,同样需要前面的图片自定义的办法。

          ProgressDialog p_dialog = new ProgressDialog(this);          p_dialog.SetMessage("正在加载……");          p_dialog.Show();            

      这个效果就是登陆或者其他的那个,如果这里用ProgressDialog.Builder 也是可以,但是要自定义显示信息,包括图片信息等等。

      5、自定义对话框

      这里自定义对话框用的还是AlertDialog,但是不同的是,自定义的对话框,要注意。自定义对话框,要完全自定义布局,也就是说,要完全定义所有的相关信息,这就相当于我们做web的时候,填出一个提示框一样,在Android里面,要完全弹出自定义对话框,哪就需要View,因为所有的界面都是View,直接右键添加一个Android Layout就可以,哇咔咔,继续开始设计。

      我的界面是这样定义的:

<?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="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="系统提示"        android:background="#0094FF"        android:textColor="#ffffff" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:background="#848484">    <[email protected]:drawable/stat_notify_missed_call 引用的是系统的图片-->        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@android:drawable/stat_notify_missed_call" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="一个未接电话" />    </LinearLayout></LinearLayout>

      OK,这里要注意一点 关于 ImageView 的 src 的问题android:src="@drawable/myapk" 这么写,引用的本地定义的myapk的图片,最好能是 png、jpg等等此类的,要是gif的好像还是要重新加载一下,这里的引用也就是自己在 drawable 文件夹添加的图片的 名字。@android:drawable/stat_notify_missed_call 这么写就是引用的 Android SDK文件夹下面的 drawable 的文件,这些文件你得先找到你自己的文件安装路径,也就是你定义的SDK的安装路径,找到安装路径之后,platformsandroid-15→data→res→drawable-ldpi 在这个文件夹下面,你就可以看到很多图片了,如果实在找不到,你还是用Android Studio安装的话,我告诉一个好消息,SDK Manager (sdk管理器)或者 AVD Manager (虚拟机管理器)的快捷方式,找到安装的根目录,这个可以做到吧。

      如图,找到对应的文件夹,依次按照platforms→android-15→data→res→drawable-ldpi 这个顺序往下点击就可以了,你就能看到你想要的了。

      后台代码:

                    View view = LayoutInflater.From(this).Inflate(Resource.Layout.MyDialog, null);                    AlertDialog.Builder a_bulid = new AlertDialog.Builder(this);                    a_bulid.SetMessage("自定义对话框").SetView(view);                    a_bulid.Show();

      先用View来接收我刚定义的界面,然后给这个界面设置一个标题,然后直接用 AlertDialog 直接 Show 就可以,其中LayoutInflater 这就是一点要注意的,这个和本身的 FindViewById就是一个相同的意思,一个找布局文件,一个找界面元素。

      6、列表对话框

      列表对话框用的还是AlertDialog

      

 AlertDialog.Builder ad_build = new AlertDialog.Builder(this)                             .SetTitle(Resource.String.warming)//标题(警告)                             .SetItems(new string[] { "中国", "日本", "韩国" }, this)                             .SetNegativeButton("确定", this)                             .SetPositiveButton("取消", this)                             .SetIcon(Android.Resource.Drawable.StatSysWarning);                    ad_build.Show();

      这里不同的就是列表对话框用的是SetItems 这个属性

    二、提示信息

      1、普通提示信息

                    var item = Toast.MakeText(this, Resource.String.info, ToastLength.Short);                    //设置垂直水平居中                    item.SetGravity(GravityFlags.CenterHorizontal | GravityFlags.CenterVertical, 0, 0);                    item.Show();

      这里的其实没有什么注意的,就是一个SetGravity 设置显示的位置的属性。

      2、含图片提示信息

            var item = Toast.MakeText(this, Resource.String.Img_Info, ToastLength.Short);                    //创建一个图片视图                    ImageView iv = new ImageView(this);                    iv.SetImageResource(Android.Resource.Drawable.StatNotifyMissedCall);                    //得到Toast布局(强制改变为线型布局)                    LinearLayout toastView = (LinearLayout)item.View;                    //设置内容显示位置                    toastView.SetGravity(GravityFlags.Center);                    //设置布局的方向                    //                    //Orientation.Horizontal 居于屏幕下方                    //Orientation.Horizontal | Orientation.Vertical                    //                    toastView.Orientation = Orientation.Horizontal;                    //给布局添加一个视图,并且设置位置                    toastView.AddView(iv, 0);                    //显示Toast                    item.Show();

      3、完全自定义提示信息

                    View view = LayoutInflater.From(this).Inflate(Resource.Layout.MyDialog, null);                    Toast toast = new Toast(this);                    toast.View = view;                    toast.Show();

      看到这里,相信大家都有一个简单的了解了,我做了一个简单的反思,就是Android的这个东西,当你要呈现一个新的元素或者其他的任务之类的,都需要去单独接受,感觉和委托的意思一样,是这样吗?

二、布局

     在android里面,不同的像素密度,一般使用的是dip做单位的,文字使用的是sp

AbsoluteLayout 绝对布局(所有的信息都是写死的)

FramerLayout 帧布局 这个布局所有的东西都是从左上角开始的,就是会叠加显示,不会像div那样挤压

LinearLayout 线程布局(默认)默认从上到下依次
android:orientation 设置布局方向
horizontal 水平均分
layout_weight 在相同的情况下,呈现的是正好是对立的状态,在同一个线型布局里面就可以看到,

带有layout_的都指的的是父空间的样式
如 layout_gravity 指的是自己在父控件里面对齐样式 gravity 就是本身自己的样式

RelativeLayout 该布局是参照父控件或者是其他控件的位置进行布局。比如说我要把A控件 ,放到B控件的下面,并且A控件的右边与B的左边对齐。类似这样的布局就可以使用相对布局来完成比较容易

fill_parent 填满当前视图,在2.2之后的android版本 match_parent 即可(相同的意思)
wrap_content 设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。

    TableLayout 布局页面

     单元格属性:
  android:layout_column:指定该单元格在第几列显示(从0开始)
  android:layout_span:跨列(意思就是当前的控件占据单元格多少列)

  列属性:
  android:stretchColumns 设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。
  android:shrinkColumns 设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。
  android:collapseColumns 设置要隐藏的列。

    本人对于布局就是个菜,本来就很不擅长做布局,No,不能用这个词,用这个词瞬间拉低了擅长这个词的身价,这样吧,我自己摸索的几个简单的布局,可以大家参考,参考

<?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="wrap_content"    android:orientation="horizontal"    android:id="@+id/absoluteLayout1"><!--android:orientation 设置的线型布局的方向--><!--在android里面,不同的像素密度,一般使用的是dip做单位的,文字使用的是sp--><!--layout_weight 在相同的情况下,呈现的是正好是对立的状态-->    <Button        android:id="@+id/MyButton1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="按钮一"        android:layout_weight="3" />    <Button        android:id="@+id/MyButton2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="按钮二"        android:layout_weight="3" />    <Button        android:id="@+id/MyButton3"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="按钮三"        android:layout_weight="3" /></LinearLayout>

效果图:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <EditText        android:id="@+id/edit"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="下面有个按钮" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮"        android:layout_below="@id/edit"        android:layout_alignRight="@id/edit" /></RelativeLayout>

效果图

来个带后台代码的。  

  

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"><!--wrap_content 包裹内容--><!--horizontal 水平--><!--android:layout_height="wrap_content" 横向包裹内容-->    <LinearLayout        android:orientation="horizontal"        android:layout_width="fill_parent"        android:layout_height="wrap_content">        <Button            android:id="@+id/home"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="首页" />        <Button            android:id="@+id/jiankang"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="第二个页面" />        <Button            android:text="第三个"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:id="@+id/button1" />    </LinearLayout>    <FrameLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent">        <LinearLayout            android:id="@+id/layout1"            android:layout_width="fill_parent"            android:layout_height="fill_parent">            <TextView                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:text="第一个布局"                android:background="#0094ff" />        </LinearLayout>        <LinearLayout            android:id="@+id/layout2"            android:layout_width="fill_parent"            android:layout_height="fill_parent">            <TextView                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:text="第二个布局"                android:background="#0045ff" />        </LinearLayout>        <LinearLayout            android:id="@+id/layout3"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="vertical">            <TextView                android:layout_width="wrap_content"                android:layout_height="match_parent"                android:text="第三个布局"                android:textSize="24sp"                android:id="@+id/tv1" />            <TextView                android:layout_width="wrap_content"                android:layout_height="match_parent"                android:textSize="36sp"                android:id="@+id/tv2"                android:gravity="center" />        </LinearLayout>    </FrameLayout></LinearLayout>

后台代码

    public class MainActivity : Activity, View.IOnClickListener    {        public void OnClick(View v)        {            LinearLayout layout3 = FindViewById<LinearLayout>(Resource.Id.layout3);            LinearLayout layout2 = FindViewById<LinearLayout>(Resource.Id.layout2);            LinearLayout layout1 = FindViewById<LinearLayout>(Resource.Id.layout1);            if (v.Id == Resource.Id.home)            {                layout1.Visibility = ViewStates.Visible;                layout2.Visibility = ViewStates.Invisible;                layout3.Visibility = ViewStates.Invisible;            }            if (v.Id == Resource.Id.jiankang)            {                layout2.Visibility = ViewStates.Visible;                layout1.Visibility = ViewStates.Invisible;                layout3.Visibility = ViewStates.Invisible;            }            if (v.Id == Resource.Id.button1)            {                layout3.Visibility = ViewStates.Visible;                layout1.Visibility = ViewStates.Invisible;                layout2.Visibility = ViewStates.Invisible;            }        }        protected override void OnCreate(Bundle bundle)        {            base.OnCreate(bundle);            // Set our view from the "main" layout resource            //  RelativeLayout            //该布局是参照父控件或者是其他控件的位置进行布局。比如说我要把A控件            //放到B控件的下面,并且A控件的右边与B的左边对齐。类似这样的布局就可            //以使用相对布局来完成比较容易            SetContentView(Resource.Layout.line);            /*            // 相对于给定ID控件                android:layout_above 将该控件的底部置于给定ID的控件之上;                android:layout_below 将该控件的底部置于给定ID的控件之下;                android:layout_toLeftOf    将该控件的右边缘与给定ID的控件左边缘对齐;                android:layout_toRightOf  将该控件的左边缘与给定ID的控件右边缘对齐;                 android:layout_alignBaseline  将该控件的baseline与给定ID的baseline对齐;                android:layout_alignTop        将该控件的顶部边缘与给定ID的顶部边缘对齐;                android:layout_alignBottom   将该控件的底部边缘与给定ID的底部边缘对齐;                android:layout_alignLeft        将该控件的左边缘与给定ID的左边缘对齐;                android:layout_alignRight      将该控件的右边缘与给定ID的右边缘对齐;                // 相对于父组件                android:layout_alignParentTop      如果为true,将该控件的顶部与其父控件的顶部对齐;                android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;                android:layout_alignParentLeft      如果为true,将该控件的左部与其父控件的左部对齐;                android:layout_alignParentRight    如果为true,将该控件的右部与其父控件的右部对齐;                // 居中                android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;                android:layout_centerVertical     如果为true,将该控件的置于垂直居中;                android:layout_centerInParent   如果为true,将该控件的置于父控件的中央;                // 指定移动像素                android:layout_marginTop      上偏移的值;                android:layout_marginBottom 下偏移的值;                android:layout_marginLeft   左偏移的值;                android:layout_marginRight   右偏移的值;            */            /*            设置line简单布局            Introduce/Test007 布局说明.txt            */            //SetContentView(Resource.Layout.line);            //layoutExample();        }        /// <summary>        /// Line布局        /// </summary>        private void layoutExample()        {            Button btn = FindViewById<Button>(Resource.Id.home);            btn.SetOnClickListener(this);            Button btn1 = FindViewById<Button>(Resource.Id.jiankang);            btn1.SetOnClickListener(this);            Button bt1 = FindViewById<Button>(Resource.Id.button1);            bt1.SetOnClickListener(this);        }    }

    原谅我,神呀,就这样了,对了,给大家留下点源码,解压之后,可以看到有一个test007和test011,其中011包含了前面所说到的所有的提示信息和消息框。007里面就是一些让正常人看着头疼的布局,额,相信我,好吧,不过你可以运行一下007的源码,点击一下那几个按钮,额,还是可以的么。

百度网盘链接:http://pan.baidu.com/s/1jHpbEoI 密码:afqf

前面两篇随笔的地址:

Android(Xamarin)之旅(一)

Android(Xamarin)之旅(二)

更多相关文章

  1. android 机顶盒开发-----GridView
  2. Android属性动画上手实现各种动画效果,自定义动画,抛物线等
  3. 2013年01月06日
  4. Android(安卓)主流屏幕以及适配
  5. Android工具HierarchyViewer 代码导读
  6. 如何在Eclipse和Android(安卓)Studio中导入library project
  7. android布局之selector(背景选择器)[转]
  8. Android(安卓)UI系列之3D星体旋转效果
  9. Android之Intent(一)

随机推荐

  1. android 几何图形的绘制
  2. Android(安卓)Web Server
  3. 在Eclipse添加Android兼容包( v4、v7 app
  4. 移植SlidingMenu Android(安卓)library,和
  5. This text field does not specify an in
  6. 如何将一个 android工程作为依赖工程
  7. Android之getSystemService
  8. Android属性之build.prop生成过程分析
  9. android 动画
  10. Android中的Handler, Looper, MessageQue