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

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. MJPhotoBrowser BUG修复

    崩溃在loading.progress = (float)receivedSize/expectedSize; 分析:MJPhotoView 执行了hide移除了MJPhotoLoadingView, ...

  2. QT5.2.1大BUG

    本来以为5.2.1是release版本 谁知道编译某个程序,执行老是crash 换5.3.2就ok了. 坑啊

  3. rdlc报表DEMO

    rdlc报表demo  .net 4.0  vs2013 文本框,图像控件,checkbox样式的打印 下载链接

  4. HBase安装及简单使用

    通过之前的hadoop0.20.2的安装并调试成功,接下来我们继续安装hbase0.90.5.在安装hbase0.90.5之前,因为hbase0.90.5只支持jdk1.6,所以,我把之前的jdk1. ...

  5. 解决MVC中JSON字符长度超出限制的异常

    解决MVC中JSON字符长度超出限制的异常 解决方法如下: <configuration> <system.web.extensions> <scripting> ...

  6. Ubuntu下deb包的安装方法 (zz)

    Ubuntu下deb包的安装方法 分类: Ubuntu10使用技巧 2010-10-11 23:49 42969人阅读 评论(3) 收藏 举报 ubuntudebdebianlinux deb是deb ...

  7. 关于“float”的一次探索--遇到了一个span元素可以设置宽高引发的思考

    起初,这个问题和float还有设置宽高之间是没有任何关联的,一开始这是一个关于height和line-height的问题,目的是为了探究一下这两者之间的关系,但是在学习的过程中,我翻之前写的代码,发现 ...

  8. Process类

    public bool Process.Start () 启动(或重用)此 Process 组件的 StartInfo 属性指定的进程资源,并将其与该组件关联. public bool Process ...

  9. 【温故Delphi】GAEA用到Win32 API目录

    Delphi是Windows平台下著名的快速应用程序开发工具,它在VCL中封装并使用了大量的Win32 API. GAEA基于VCL开发的工具类产品,在程序中使用了大量的Win32 API,将经常用到 ...

  10. HTML5中新添加事件

    HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...