Home  >  Article  >  php教程  >  [Android] Chapter 7 (1) Manifest, Adapter and Layout--Make your program more elegant

[Android] Chapter 7 (1) Manifest, Adapter and Layout--Make your program more elegant

WBOY
WBOYOriginal
2016-07-06 13:30:441123browse

Category: C#, Android, VS2015; Creation date: 2016-02-09 1. Configure the application manifest in the AssemblyInfo.cs file. As we said in the previous chapter, in addition to configuring the application manifest in the AndroidManifest.xml file , you can also configure the application manifest in the AssemblyInfo.cs file. Configure in AssemblyInfo.cs file

Category: C#, Android, VS2015;

Creation date: 2016-02-09

1. Configure the application manifest in the AssemblyInfo.cs file

front In the chapter, we said that in addition to configuring the application manifest in the AndroidManifest.xml file, you can also configure the application manifest in the AssemblyInfo.cs file.

Configuring the application manifest in the AssemblyInfo.cs file is a recommended practice when writing applications in C#.

The advantage of configuring the application manifest in the AssemblyInfo.cs file is: due to convenient smart prompts when typing C# code, adding and modifying the manifest configuration is much easier than configuring it directly in the AndroidManifest.xml file.

In fact, when writing Android applications in C#, you can completely ignore the AndroidManifest.xml file. Let the compiler manage these internal processing processes for you. Anyway, the final generated manifest configuration result It's all the same. However, this configuration method with smart prompts is much more convenient than directly modifying the AndroidManifest.xml file.

Of course, this is just another way for the VS2015 compiler to intelligently configure the Android application list. It is also a commonly used way to configure the list in C# programming. However, if you are still used to directly modifying the AndroidManifest.xml file in the project like Java programming, and you can also tolerate inexplicable errors caused by inconsistent configurations, you can also continue to use the method of directly modifying the AndroidManifest.xml file. Anyway, Java programming The staff are already used to changing the original configuration method in the past, and they are numb to this method. But for C# programming, this kind of stupid implementation idea of ​​letting programmers configure this configuration and that by themselves is really real. It's intolerable.

In short, I still prefer to configure it in the AssemblyInfo.cs file. This method not only allows you to use smart prompts to intuitively see what configurable options there are, but also allows you to You can see if the configuration is wrong.

Starting from this chapter, we will use this method to configure a manifest that works throughout the entire application, instead of directly modifying the AndroidManifest.xml file.

2. Create a more flexible custom adapter

In the main interface of the previous chapter, we used the simplest method: directly create a string array to list the sample navigation . Although this method is simple, it is the least flexible and stupid method.

In this section we will learn how to make the content displayed on the main interface more flexible, which is also a commonly used method in actual projects.

1. Where to define the adapter

Generally, which class the adapter targets is defined in the file containing this class. For example, to write an adapter for use in the MainActivity class, define it in the MainActivity.cs file.

2. Create your own list item class

Which list items you want to create depends on your needs. The following code demonstrates how to create the MyItems class:

public class MyItems

{

public string Title { get; set; }

public string Desc { get; set; }

}

3. Tips for creating a custom adapter

Once you have your own list item (MyItems class), you can customize it Specify it in the adapter.

The screenshot below demonstrates how to quickly create a custom adapter:

[Android] Chapter 7 (1) Manifest, Adapter and Layout--Make your program more elegant

Click the small triangle symbol to the right of the yellow light bulb in the pop-up drop-down box Select [Implement Abstract Class] and it will automatically add all the methods that need to be rewritten, without you having to type the code one by one.

Note: The "not implemented..." prompted here should actually be "not implemented...". This is a matter of Chinese translation. Just understand its actual meaning.

3. The main interface of the example in this chapter

All the source programs of the example in this chapter are in the ch07demos project.

Project name: ch07demos

Project template: Blank App(Android)

1. Running screenshots

The main interface running screenshots are as follows:

[Android] Chapter 7 (1) Manifest, Adapter and Layout--Make your program more elegant

2. Modify the target version of the release

In the solution explorer, right-click the [ch07demos] project, select [Properties], and change [Compile using Android version] option to "API Level 19", as shown below:

[Android] Chapter 7 (1) Manifest, Adapter and Layout--Make your program more elegant

3. Modify the manifest file (AssemblyInfo.cs)

Add an application-level theme to this file and other configurations. The complete content of the added AssemblyInfo.cs is as follows:

<span style="color: #0000ff">using</span><span style="color: #000000"> System.Reflection;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Runtime.CompilerServices;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Runtime.InteropServices;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> Android.App;

[assembly: AssemblyTitle(</span><span style="color: #800000">"</span><span style="color: #800000">ch07demos</span><span style="color: #800000">"</span><span style="color: #000000">)]
[assembly: AssemblyDescription(</span><span style="color: #800000">"</span><span style="color: #800000">布局控件的基本用法</span><span style="color: #800000">"</span><span style="color: #000000">)]
[assembly: AssemblyConfiguration(</span><span style="color: #800000">""</span><span style="color: #000000">)]
[assembly: AssemblyCompany(</span><span style="color: #800000">"</span><span style="color: #800000">毛毛雨的博客乐园(http://www.cnblogs/rainmj/)</span><span style="color: #800000">"</span><span style="color: #000000">)]
[assembly: AssemblyPRoduct(</span><span style="color: #800000">"</span><span style="color: #800000">rainmjAndroidDemos</span><span style="color: #800000">"</span><span style="color: #000000">)]
[assembly: AssemblyCopyright(</span><span style="color: #800000">"</span><span style="color: #800000">Copyright ?  2016</span><span style="color: #800000">"</span><span style="color: #000000">)]
[assembly: AssemblyTrademark(</span><span style="color: #800000">""</span><span style="color: #000000">)]
[assembly: AssemblyCulture(</span><span style="color: #800000">"</span><span style="color: #800000">zh-CN</span><span style="color: #800000">"</span><span style="color: #000000">)]
[assembly: ComVisible(</span><span style="color: #0000ff">false</span><span style="color: #000000">)]

[assembly:application(Theme </span>= <span style="color: #800000">"</span><span style="color: #800000">@android:style/Theme.DeviceDefault.Light</span><span style="color: #800000">"</span><span style="color: #000000">)]

[assembly: AssemblyVersion(</span><span style="color: #800000">"</span><span style="color: #800000">1.0.*</span><span style="color: #800000">"</span>)]

4. Modify the main interface (Main.axml)

Change Main.axml to the following code:

<span style="color: #0000ff"></span><span style="color: #ff00ff">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff">?></span>
<span style="color: #0000ff"><span style="color: #800000">LinearLayout </span><span style="color: #ff0000">xmlns:android</span><span style="color: #0000ff">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000">
    android:orientation</span><span style="color: #0000ff">="vertical"</span><span style="color: #ff0000">
    android:layout_width</span><span style="color: #0000ff">="fill_parent"</span><span style="color: #ff0000">
    android:layout_height</span><span style="color: #0000ff">="fill_parent"</span><span style="color: #0000ff">></span>
    <span style="color: #0000ff"><span style="color: #800000">ListView
        </span><span style="color: #ff0000">android:minWidth</span><span style="color: #0000ff">="25px"</span><span style="color: #ff0000">
        android:minHeight</span><span style="color: #0000ff">="25px"</span><span style="color: #ff0000">
        android:layout_width</span><span style="color: #0000ff">="match_parent"</span><span style="color: #ff0000">
        android:layout_height</span><span style="color: #0000ff">="match_parent"</span><span style="color: #ff0000">
        android:id</span><span style="color: #0000ff">="@+id/listView1"</span> <span style="color: #0000ff">/></span>
<span style="color: #0000ff"></span><span style="color: #800000">LinearLayout</span><span style="color: #0000ff">></span></span></span>

5. Modify the main activity file (MainActivity.cs)

After all the examples in this chapter are completed, the code of MainActivity.cs is as follows:

<span style="color: #0000ff">using</span><span style="color: #000000"> Android.App;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> Android.Views;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> Android.Widget;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> Android.OS;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> System.Collections.Generic;
</span><span style="color: #0000ff">using</span><span style="color: #000000"> ch07demos.SrcDemos;

</span><span style="color: #0000ff">namespace</span><span style="color: #000000"> ch07demos
{
    [Activity(Label </span>= <span style="color: #800000">"</span><span style="color: #800000">ch07demos</span><span style="color: #800000">"</span>, MainLauncher = <span style="color: #0000ff">true</span>, Icon = <span style="color: #800000">"</span><span style="color: #800000">@drawable/icon</span><span style="color: #800000">"</span><span style="color: #000000">)]
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> MainActivity : Activity
    {
        </span><span style="color: #0000ff">protected</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span><span style="color: #000000"> OnCreate(Bundle bundle)
        {
            </span><span style="color: #0000ff">base</span><span style="color: #000000">.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
            List</span><myitems> items = <span style="color: #0000ff">new</span> List<myitems><span style="color: #000000">()
            {
                </span><span style="color: #0000ff">new</span> MyItems {Title=<span style="color: #800000">"</span><span style="color: #800000">Demo01--LinearLayout</span><span style="color: #800000">"</span>,Desc=<span style="color: #800000">"</span><span style="color: #800000">演示线性布局的基本用法</span><span style="color: #800000">"</span><span style="color: #000000"> },
                </span><span style="color: #0000ff">new</span> MyItems {Title=<span style="color: #800000">"</span><span style="color: #800000">Demo02--GridLayout</span><span style="color: #800000">"</span>,Desc=<span style="color: #800000">"</span><span style="color: #800000">演示网格布局的基本用法</span><span style="color: #800000">"</span><span style="color: #000000"> },
                </span><span style="color: #0000ff">new</span> MyItems {Title=<span style="color: #800000">"</span><span style="color: #800000">Demo03--TableLayout</span><span style="color: #800000">"</span>,Desc=<span style="color: #800000">"</span><span style="color: #800000">演示表格布局的基本用法</span><span style="color: #800000">"</span><span style="color: #000000"> },
                </span><span style="color: #0000ff">new</span> MyItems {Title=<span style="color: #800000">"</span><span style="color: #800000">Demo04--RelativeLayout</span><span style="color: #800000">"</span>,Desc=<span style="color: #800000">"</span><span style="color: #800000">演示相对布局的基本用法</span><span style="color: #800000">"</span><span style="color: #000000"> },
                </span><span style="color: #0000ff">new</span> MyItems {Title=<span style="color: #800000">"</span><span style="color: #800000">Demo05--FrameLayout</span><span style="color: #800000">"</span>,Desc=<span style="color: #800000">"</span><span style="color: #800000">演示帧布局的基本用法</span><span style="color: #800000">"</span><span style="color: #000000"> }
            };
            ListView listView1 </span>= FindViewById<listview><span style="color: #000000">(Resource.Id.listView1);
            listView1.Adapter </span>= <span style="color: #0000ff">new</span> MyAdapter(<span style="color: #0000ff">this</span><span style="color: #000000">, items);
            listView1.ItemClick </span>+= (s, e) =><span style="color: #000000">
            {
                </span><span style="color: #0000ff">int</span> index = e.Position + <span style="color: #800080">1</span><span style="color: #000000">;
                </span><span style="color: #0000ff">switch</span><span style="color: #000000"> (index)
                {
                    </span><span style="color: #0000ff">case</span> <span style="color: #800080">1</span><span style="color: #000000">:
                        StartActivity(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(Demo01LinearLayout));
                        </span><span style="color: #0000ff">break</span><span style="color: #000000">;
                    </span><span style="color: #0000ff">case</span> <span style="color: #800080">2</span><span style="color: #000000">:
                        StartActivity(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(Demo02GridLayout));
                        </span><span style="color: #0000ff">break</span><span style="color: #000000">;
                    </span><span style="color: #0000ff">case</span> <span style="color: #800080">3</span><span style="color: #000000">:
                        StartActivity(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(Demo03TableLayout));
                        </span><span style="color: #0000ff">break</span><span style="color: #000000">;
                    </span><span style="color: #0000ff">case</span> <span style="color: #800080">4</span><span style="color: #000000">:
                        StartActivity(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(Demo04RelativeLayout));
                        </span><span style="color: #0000ff">break</span><span style="color: #000000">;
                    </span><span style="color: #0000ff">case</span> <span style="color: #800080">5</span><span style="color: #000000">:
                        StartActivity(</span><span style="color: #0000ff">typeof</span><span style="color: #000000">(Demo05FrameLayout));
                        </span><span style="color: #0000ff">break</span><span style="color: #000000">;
                }
            };
        }
    }

    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> MyItems
    {
        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Title { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span><span style="color: #000000">; }
        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Desc { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span><span style="color: #000000">; }
    }

    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyAdapter : BaseAdapter<myitems><span style="color: #000000">
    {
        </span><span style="color: #0000ff">private</span> List<myitems><span style="color: #000000"> items;
        </span><span style="color: #0000ff">private</span><span style="color: #000000"> Activity context;

        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span><span style="color: #000000"> Count
        {
            </span><span style="color: #0000ff">get</span><span style="color: #000000">
            {
                </span><span style="color: #0000ff">return</span><span style="color: #000000"> items.Count;
            }
        }

        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> MyItems <span style="color: #0000ff">this</span>[<span style="color: #0000ff">int</span><span style="color: #000000"> position]
        {
            </span><span style="color: #0000ff">get</span><span style="color: #000000">
            {
                </span><span style="color: #0000ff">return</span><span style="color: #000000"> items[position];
            }
        }

        </span><span style="color: #0000ff">public</span> MyAdapter(Activity context, List<myitems><span style="color: #000000"> items)
        {
            </span><span style="color: #0000ff">this</span>.context =<span style="color: #000000"> context;
            </span><span style="color: #0000ff">this</span>.items =<span style="color: #000000"> items;
        }

        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">long</span> GetItemId(<span style="color: #0000ff">int</span><span style="color: #000000"> position)
        {
            </span><span style="color: #0000ff">return</span><span style="color: #000000"> position;
        }

        </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> View GetView(<span style="color: #0000ff">int</span><span style="color: #000000"> position, View convertView, ViewGroup parent)
        {
            </span><span style="color: #0000ff">var</span> item =<span style="color: #000000"> items[position];
            View view </span>= <span style="color: #0000ff">null</span><span style="color: #000000">;
            view </span>= context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem2, <span style="color: #0000ff">null</span><span style="color: #000000">);
            view.FindViewById</span><textview>(Android.Resource.Id.Text1).Text =<span style="color: #000000"> item.Title;
            view.FindViewById</span><textview>(Android.Resource.Id.Text2).Text =<span style="color: #000000"> item.Desc;
            </span><span style="color: #0000ff">return</span><span style="color: #000000"> view;
        }
    }
}</span></textview></textview></myitems></myitems></myitems></listview></myitems></myitems>

OK, that’s the first lecture of this chapter.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn