這篇我們來動態加入,一樣務求好懂簡單

1.一樣先將專案調整成3.0以上版本

2.首先建立自定Control的Layout \Resources\Layout\MyControlLayout1.axml

主要我要設定此元件有一個按鈕 按下去後,可以改變上方TextView (textView1) 的文字內容,當然這文字內容可能是由主要的Activity給予的

MyControlLayout1.axml Code :

<?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="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是Control 預設文字" />
    <Button
        android:text="Control內部按鈕"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnChangeText" />
</LinearLayout>

3. 來開一個Fragment 來操控 MyControlLayout1 的Layout \MyControlFragment.cs

MyControlFragment.cs Code :

using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
 
namespace DynamicCallFragment
{
    public class MyControlFragment : Fragment
    {
        /// <summary>
        /// Poroperty DisplayContext 
        /// 欲呈現的文字
        /// </summary>
        public string DisplayContext { get; set; }
 
 
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View v = inflater.Inflate(Resource.Layout.MyControlLayout1, container, false);
            var btnChangeText = v.FindViewById<Button>(Resource.Id.btnChangeText);
 
            //設定  btnChangeText 點擊後將 textView1 的內容設為 DisplayContext
            btnChangeText.Click += delegate
            {
                var textView1 = v.FindViewById<TextView>(Resource.Id.textView1);
                textView1.Text = DisplayContext;
            };
 
            return v;
        }
    }
}

4.主Activity Layout   \Resources\Layout\Main.axml


其中我放入一個LinearLayout (linearLayout1)拿來放生出來的Control  Main.axml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Test 動態加入Control" />
    <Button
        android:text="加入新的Fragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnDynamicAddFragment" />
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" />
</LinearLayout>

5.接下來就是 MainActivity 來做動態生成的部分拉:

using Android.App;
using Android.Widget;
using Android.OS;
 
namespace DynamicCallFragment
{
    [Activity(Label = "動態加入Control", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        private int count { get; set; }
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
 
            // --- 動態呼叫
            var btnDynamicAddFragment = FindViewById<Button>(Resource.Id.btnDynamicAddFragment);
            btnDynamicAddFragment.Click += delegate
            {
 
                var fragmentManager = FragmentManager;
                FragmentTransaction fragmentTransaction = fragmentManager.BeginTransaction();
                var d_fragment1 = new MyControlFragment();
                //加上計數作為辨識
                d_fragment1.DisplayContext = "我是動態叫起來的" + count;
                fragmentTransaction.Add(Resource.Id.linearLayout1, d_fragment1);
                fragmentTransaction.Commit();
                count++;
 
            };
        }
    }
}
 

結果: 執行起來

按下加入新的Fragment後

按下Control內部按鈕:

多測試幾個

是不是比之前單純Inflate好管理多了呢 :)

Reference: http://docs.xamarin.com/guides/android/platform_features/fragments

http://blog.kenyang.net/2013/03/android-fragment-activity.html

[Xamarin] 動態載入Fragment (转帖)的更多相关文章

  1. 篇章三:[AngularJS] 使用AngularCSS動態載入CSS

    前言 使用AngularAMD動態載入Controller 使用AngularAMD動態載入Service 上列兩篇文章裡,介紹了如何如何使用AngularAMD來動態載入Controller與Ser ...

  2. 篇章二:[AngularJS] 使用AngularAMD動態載入Service

    前言 「使用AngularAMD動態載入Controller」:這篇文章裡介紹如何使用AngularAMD來動態載入Controller.本篇文章以此為基礎,介紹如何使用AngularAMD來動態載入 ...

  3. 篇章一:[AngularJS] 使用AngularAMD動態載入Controller

    前言 使用AngularJS來開發Single Page Application(SPA)的時候,可以選用AngularUI Router來提供頁面內容切換的功能.但是在UI Router的使用情景裡 ...

  4. [Xamarin] 使用LayoutInflater.Inflate載入預先設計好的Layout並使用 (转帖)

    開發的時候,一定會把一些東西設計成元件,並且可以多次使用,今天紀錄一篇比較簡單的方法,可以載入事先做好的Layout 並且給予事件 介紹一下範例: Main.axml: <?xml versio ...

  5. JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (3):部署設定及應用 (转帖)

    說明:這一篇主要是說明如何將程式部署到Application Server,以及程式如何運作,產生的檔案置於何處,以及如何以瀏覽器呈現(Applet),或是當成桌面應用程式,或是 桌面Applet,這 ...

  6. JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (2):JavaFX建立及程式碼說明 (转帖)

    說明:就如同標題一樣,前端會用到JavaFX.Swing.Java Web Start.Google Map 的技術, 後端就是JDBC.Servlet的技術,以及我們會簽署認證jar檔案,這樣才可存 ...

  7. JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (1):NetBeans 寫 Servlet (转帖)

    JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (1):NetBeans 寫 Servlet 功能:這支程式的主要功能是將 javafx 與 swi ...

  8. Jquery easy ui datagrid動態加載列問題

    1.如下图效果是当选择不同的日期范围时datagrid则会加载出对应的列数

  9. [Xamarin] 使用Webview 來做APP (转帖)

    有時候,企業要求的沒有這麼多,他原本可能官方網站就已經有支援Mobile Web Design 他只需要原封不動的開發一個APP 也或是,他只是要型錄型,或是問卷調查的型的APP,這時候透過類似像if ...

随机推荐

  1. js+cookie 购物车

    $(function () { //var ctx = new Ch(); //ctx.Clear(); //$.cookie(ctx.cookieName, ""); //ale ...

  2. Unity3d利用opencv保存游戏视频

    脚本MyVideoWriter.cs using UnityEngine; using System.Collections; using OpenCvSharp; using OpenCvSharp ...

  3. jQuery系列之操作select标签

    每次看完东西基本就忘了,现在决定写一下博客来记录,不知道效果咋样. 一.jQuery操作选择器 1.基本选择器 关于基本选择器,我就不用太多说了,包括了ID.类.标签等选择器. 2.层次选择器 我觉得 ...

  4. 一台独立的服务器是可以可以建立多个网站的,一个ip地址,一个端口

    # 1,若开启虚拟主机,这个一定要有(IP:端口),找到 #NameVirtualHost *:80 修改成: NameVirtualHost 127.0.0.1:80 # 2,修改<Direc ...

  5. 三维模型2.5D轮廓提取及遮挡部分的剔除

    轮廓提取相对容易,只需在2.5D渲染视角下,导出模型的顶点坐标以及基于视角的消隐后的三角形面,将三角面投影后合并就可得到轮廓,轮廓坐标基于2.5d图的基准坐标换算就得到.提取轮廓的在我另外一篇文章中有 ...

  6. c# 导出数据到Excel模板

    最近在做一个发邮件的功能,客户要求需要导出一个Excel附件,并给了附件的格式, eg: Last Name 姓 First Name 名 Chinese Characters汉字书写(仅大陆人填写) ...

  7. ubuntu 12.10 sourcelist软件更新源列表(zz)

        ubuntu 12.10正式版已经发布了,国内各大开源软件源也陆续更新了资源.今天分享一下ubuntu 12.10 软件更新源列表. 首先,备份一下ubuntu 12.04 原来的源地址列表文 ...

  8. 一个书店管理系统java

    自己的第一个小程序 ps:书是在集合里面后面文件处理的有一点小问题,希望有人会给点意见 //客户类 import java.io.Serializable; public class Customer ...

  9. AngularJS之ng-options的best practise

    废话不多说,直接上代码. function MySelectCtrl($scope) { $scope.Model = [ { id: 10002, MainCategory: '男', Produc ...

  10. windows sdk编程 richedit创建,像十六进制编辑器一样显示文件

    编译环境 :windows 7 64位 vs2010,工程创建选择"win32项目" 注意添加几个头文件 #include <WinBase.h> #include & ...