Xamarin.Android之簡單的抽屜布局


0x01 前言

相信對於用過Android版QQ的,應該都不會陌生它那個向右滑動的菜單(雖說我用的是Lumia)

今天就用Xamarin.Android實現個比較簡單的抽屜布局。下面直接進正題。

0x02 做個簡單的抽屜布局

新建個android項目

通過NuGet安裝個Xamarin.Android.Support.v4

其實呢,官網那里還用很多組件可用拿來嘗試一下的。

https://components.xamarin.com/

然后修改Main.axml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     <android.support.v4.widget.DrawerLayout
 6         android:id="@+id/mDrawerLayout"
 7         android:layout_width="match_parent"
 8         android:layout_height="match_parent">
 9         <FrameLayout
10             android:id="@+id/content_frame"
11             android:layout_width="match_parent"
12             android:layout_height="match_parent" />
13         <ListView
14             android:id="@+id/left_drawer"
15             android:layout_width="200dp"
16             android:layout_height="match_parent"
17             android:layout_gravity="start"
18             android:background="@android:color/holo_blue_light"
19             android:choiceMode="singleChoice"
20             android:divider="@android:color/transparent"
21             android:dividerHeight="0dp" />
22     </android.support.v4.widget.DrawerLayout>
23 </RelativeLayout>

這里用了相對布局,更重要的是android.support.v4.widget.DrawerLayout

同時新建一個fragmentcontent.axml,用於呈現選中菜單的內容。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6     <TextView
 7         android:layout_width="match_parent"
 8         android:layout_height="match_parent"
 9         android:gravity="center"
10         android:textAlignment="center"
11         android:textSize="30dp"
12         android:id="@+id/txtName" />
13 </LinearLayout>

內容比較簡單,就是顯示相應菜單的文本。

然后,修改MainActivity

 1 using Android.App;
 2 using Android.OS;
 3 using Android.Support.V4.Widget;
 4 using Android.Widget;
 5 namespace DrawerLayoutDemo
 6 {
 7     [Activity(Label = "DrawerLayoutDemo", MainLauncher = true, Icon = "@drawable/icon")]
 8     public class MainActivity : Activity
 9     {
10         private string[] _menu;
11         protected override void OnCreate(Bundle bundle)
12         {
13             base.OnCreate(bundle);
14             
15             SetContentView(Resource.Layout.Main);
16             //the menu
17             _menu = new string[] { "C#", "Python", "Xamarin" };
18             //listview
19             var listView = FindViewById<ListView>(Resource.Id.left_drawer);
20             //adapter
21             listView.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, _menu);
22             //drawerlayout
23             var drawerLayout = FindViewById<DrawerLayout>(Resource.Id.mDrawerLayout);
24             //click event
25             listView.ItemClick += ItemClick;
26         }
27         /// <summary>
28         /// item click event of the listview 
29         /// </summary>
30         /// <param name="sender"></param>
31         /// <param name="e"></param>
32         private void ItemClick(object sender, AdapterView.ItemClickEventArgs e)
33         {  
34             //fragment
35             Fragment fragment = new FragmentContent(_menu[e.Position]);
36             //show
37             var fm = FragmentManager.BeginTransaction().Replace(Resource.Id.content_frame, fragment).Commit();
38         }
39     }
40 }

MainActivity的話主要是處理ListView的綁定以及點擊事件。

新建一個FragmentContent

 1 using Android.App;
 2 using Android.OS;
 3 using Android.Views;
 4 using Android.Widget;
 5 namespace DrawerLayoutDemo
 6 {
 7     public class FragmentContent : Fragment
 8     {
 9         private string _text;
10         public FragmentContent(string text)
11         {            
12             _text = text;
13         }
14         public override void OnCreate(Bundle savedInstanceState)
15         {
16             base.OnCreate(savedInstanceState);            
17         }
18                 
19         public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
20         {
21             //get the view
22             View view = inflater.Inflate(Resource.Layout.fragmentcontent, null);            
23             var txt = view.FindViewById<TextView>(Resource.Id.txtName);
24             //set the text of the textview
25             txt.Text = "I Love " + _text;
26             return view;
27         }
28     }
29 }

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/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM