【Android】5.0 第5章 常用基本控件--本章示例主界面

分类:C#、Android、VS2015;

创建日期:2016-02-06

这一章主要介绍Android简单控件的基本用法。本章源程序共有9个示例,这些示例都在同一个项目中。

项目名:ch05demos,项目模板:Blank App(Android)

运行主界面截图如下:

image

点击每行的示例项,即进入对应示例的页面。

1、在drawable文件夹下添加图片

添加的图片见下面的左图,也可以直接拖放图片到drawable文件夹下。

右图是各节例子实现后的纵向屏幕布局文件(layout文件夹)、横向屏幕布局文件(layout-land文件夹)、弹出菜单布局文件(menu文件夹)。这些都是在本章后续将要介绍的节中添加的。

image

2、主界面(Main.axml)

修改后的代码如下:

<?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">
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView1" />
</LinearLayout>

3、主界面对应的活动文件(MainActivity.cs)

本章示例全部完成后MainActivity.cs的代码如下:

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using ch05demos.SrcActivity;

namespace ch05demos
{
    [Activity(Label = "ch05demos", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        string[] items;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            items = new string[]
            {
                "Demo01EditText",
                "Demo02Login",
                "Demo03MultiResolution",
                "Demo04CheckBoxRadioButton",
                "Demo05Spinner",
                "Demo06SwitchAndRatingBar",
                "Demo07PopupMenu",
                "Demo08Gallery",
                "Demo09SeekBar"
            };
            ListView listView1 = FindViewById<ListView>(Resource.Id.listView1);
            listView1.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
            listView1.ItemClick += (s, e) =>
            {
                int index = e.Position + 1;
                switch(index)
                {
                    case 1:
                        StartActivity(typeof(Demo01EditText));
                        break;
                    case 2:
                        StartActivity(typeof(Demo02Login));
                        break;
                    case 3:
                        StartActivity(typeof(Demo03MultiResolution));
                        break;
                    case 4:
                        StartActivity(typeof(Demo04CheckBoxRadioButton));
                        break;
                    case 5:
                        StartActivity(typeof(Demo05Spinner));
                        break;
                    case 6:
                        StartActivity(typeof(Demo06SwitchAndRatingBar));
                        break;
                    case 7:
                        StartActivity(typeof(Demo07PopupMenu));
                        break;
                    case 8:
                        StartActivity(typeof(Demo08Gallery));
                        break;
                    case 9:
                        StartActivity(typeof(Demo09SeekBar));
                        break;
                }
            };
        }
    }
}

注意:直接定义字符串数组这种方式虽然用法最简单易理解,但并不是最好的办法,下一章再介绍比这个稍微好一点的办法(但仍然不是最好的办法,别着急,一步一步来,要不你没有比较怎么知道哪个好哪个坏呢)。

4、清单文件(AndroidManifest.xml)

在这个文件中只添加了一条内容:设置应用到所有页面的公用主题。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ch05demos.ch05demos" android:versionCode="1" android:versionName="1.0">
    <uses-sdk />
    <application android:label="ch05demos"
       android:theme="@android:style/Theme.DeviceDefault.Light">
</application>
</manifest>

从下一节开始,将分别介绍如何实现各个示例,以及这些示例涉及的相关概念。

原文地址:https://www.cnblogs.com/rainmj/p/5184156.html