[Xamarin] 動態載入Fragment (转帖)
這篇我們來動態加入,一樣務求好懂簡單
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 (转帖)的更多相关文章
- 篇章三:[AngularJS] 使用AngularCSS動態載入CSS
前言 使用AngularAMD動態載入Controller 使用AngularAMD動態載入Service 上列兩篇文章裡,介紹了如何如何使用AngularAMD來動態載入Controller與Ser ...
- 篇章二:[AngularJS] 使用AngularAMD動態載入Service
前言 「使用AngularAMD動態載入Controller」:這篇文章裡介紹如何使用AngularAMD來動態載入Controller.本篇文章以此為基礎,介紹如何使用AngularAMD來動態載入 ...
- 篇章一:[AngularJS] 使用AngularAMD動態載入Controller
前言 使用AngularJS來開發Single Page Application(SPA)的時候,可以選用AngularUI Router來提供頁面內容切換的功能.但是在UI Router的使用情景裡 ...
- [Xamarin] 使用LayoutInflater.Inflate載入預先設計好的Layout並使用 (转帖)
開發的時候,一定會把一些東西設計成元件,並且可以多次使用,今天紀錄一篇比較簡單的方法,可以載入事先做好的Layout 並且給予事件 介紹一下範例: Main.axml: <?xml versio ...
- JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (3):部署設定及應用 (转帖)
說明:這一篇主要是說明如何將程式部署到Application Server,以及程式如何運作,產生的檔案置於何處,以及如何以瀏覽器呈現(Applet),或是當成桌面應用程式,或是 桌面Applet,這 ...
- JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (2):JavaFX建立及程式碼說明 (转帖)
說明:就如同標題一樣,前端會用到JavaFX.Swing.Java Web Start.Google Map 的技術, 後端就是JDBC.Servlet的技術,以及我們會簽署認證jar檔案,這樣才可存 ...
- JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (1):NetBeans 寫 Servlet (转帖)
JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (1):NetBeans 寫 Servlet 功能:這支程式的主要功能是將 javafx 與 swi ...
- Jquery easy ui datagrid動態加載列問題
1.如下图效果是当选择不同的日期范围时datagrid则会加载出对应的列数
- [Xamarin] 使用Webview 來做APP (转帖)
有時候,企業要求的沒有這麼多,他原本可能官方網站就已經有支援Mobile Web Design 他只需要原封不動的開發一個APP 也或是,他只是要型錄型,或是問卷調查的型的APP,這時候透過類似像if ...
随机推荐
- redirect和forward
1.重定向 <mvc:view-controller path="/" view-name="redirect:/admin/index"/>即如果 ...
- Quarter square 查找表乘法器,手动建立rom
建立一个C的范围为0~255,内容是(C)2/4的查表 占用256个存储空间,但可以计算出+-127的两个数之积.传统算法需要至少127×127个存储空间. 查找表模块的建立: module lut_ ...
- c# Exception 异常信息归整
private string ErrorMessage(Exception exception) { StringBuilder stringBuilder = new StringBuilder() ...
- 主机使用代理上网,虚拟机Linux如何连外网
VMware虚拟机的三种联网方法及原理 一.Brigde--桥接 :默认使用VMnet0 1.原理: Bridge 桥"就是一个主机,这个机器拥有两块网卡,分别处于两个局域网中,同时在& ...
- runtime 运行机制2
Mike_zh QQ:82643885 end: blogTitle 博客的标题和副标题 博客园 首页 新随笔 联系 订阅 <a id="MyLinks1_XMLLink" ...
- 关于“float”的一次探索--遇到了一个span元素可以设置宽高引发的思考
起初,这个问题和float还有设置宽高之间是没有任何关联的,一开始这是一个关于height和line-height的问题,目的是为了探究一下这两者之间的关系,但是在学习的过程中,我翻之前写的代码,发现 ...
- myeclipse中UTF-8设置
myeclipse中UTF-8设置 如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,Eclipse工作空间(workspace ...
- android 模拟器上网问题
android 模拟器上网问题 1.配置Adroid环境变量(Win7为例) ,启动模拟器 第一步:桌面右键——>我的电脑——>高级系统设置 第二步:高级——>环境变量——&g ...
- QQ浏览器安卓5.8版本的Uint8Array API有bug
调用new Uint8Array()时QQ浏览器将直接返回参数列表, 比如new Uint8Array(a)将返回[a],比如new Uint8Array(a, b)将返回[a, b],比如new U ...
- unity 实现物体破碎效果的一些方法
游戏越来越接近现实的感觉,如果有一个真是的 虚拟现实设备,可能我们真的会感觉是在真实世界.场景的逼真是在渲染效果.角色AI.游戏逻辑.物理效果等等一起导致的结果.现在游戏越来越大,除了渲染,物理估计是 ...