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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/btn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Inflate 客製化 Layout" />
    <LinearLayout
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/relativeContainer"
        android:orientation="vertical" />
</LinearLayout>

紅色框起來的部分,就是我預留給客製化Layout放的地方,id 為 custContainer 的 LinearLayout 讓自訂的Layout可以被Inflate(打氣)在這地方 "Inflate  客製 Layout" 按鈕被按下時,會Inflate 一個客製化的元件至custContainer,並且給愈該元件該有的事件 介紹一下,這是我客製化的Layout


CustControlLayout.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <Button
        android:text="我是Control的按鈕"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnAssignText" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="我是Control的內容"
        android:id="@+id/editTextContent" />
</LinearLayout>

這是我設計的客製化Layout 功能是我希望btnAssignText 被點擊之後,可以Toast  editTextContent  我們來看看,如何在主Activity 中將 CustControlLayout 載入到 custContainer 中並給予事件

using System;
using Android.App;
using Android.Widget;
using Android.OS;
 
namespace TestInflate
{
    [Activity(Label = "TestInflate", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
 
            var btn1 = FindViewById<Button>(Resource.Id.btn1);
 
            var custContainer = FindViewById<LinearLayout>(Resource.Id.custContainer);
 
            btn1.Click += delegate
            {
                //透過 LayoutInflater 將 CustControlLayout 打氣在 custContainer 容器中
                LayoutInflater.Inflate(Resource.Layout.CustControlLayout, custContainer);
 
                var btnAssignText = FindViewById<Button>(Resource.Id.btnAssignText);
                //重新給予一個亂數的Id 不然啟動後事件都會是同一個
                btnAssignText.Id = new Random().Next(5000, int.MaxValue);
 
                var editTextContent = FindViewById<EditText>(Resource.Id.editTextContent);
                //重新給予一個亂數的Id 不然啟動後事件都會是同一個
                editTextContent.Id = new Random().Next(5000, int.MaxValue);
 
                btnAssignText.Click += delegate
                    {
                        Toast.MakeText(this, editTextContent.Text, ToastLength.Short).Show();
                    };
 
 
            };
        }
    }
}
 

結果:


載入的第二個按下時:

References: http://lp43.blogspot.tw/2010/06/xmllayoutviewlayoutinflater.html http://developer.android.com/reference/android/view/LayoutInflater.html
http://blog.csdn.net/hwy584624785/article/details/6695301

[Xamarin] 使用LayoutInflater.Inflate載入預先設計好的Layout並使用 (转帖)的更多相关文章

  1. 何解決 LinqToExcel 發生「無法載入檔案或組件」問題何解決 LinqToExcel 發生「無法載入檔案或組件」問題

    在自己的主機上透過 Visual Studio 2013 與 IISExpress 開發與測試都還正常,但只要部署到測試機或正式機,就是沒辦法順利執行,卡關許久之後找我協助.我發現錯誤訊息確實很「一般 ...

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

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

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

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

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

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

  5. 在 React Native 中使用 moment.js 無法載入語系檔案

    moment.js 是很常見的日期時間 library,友善的 API 與極佳的執行效率是它的兩大賣點.例如 (new Date()).getFullYear(),如果使用 moment.js 我可以 ...

  6. 6、Android之LayoutInflater.inflate()

    LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...

  7. android LayoutInflater.inflate()的参数介绍

    LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...

  8. Android LayoutInflater.inflate使用上的问题解惑

    最近在在使用LayoutInflater.inflate方法时遇到了一些问题,以前没有仔细看过此类的使用方法,故将其记录下来,方便日后查阅. 相信大家都知道LayoutInflater.inflate ...

  9. setContentView()与LayoutInflater.inflate()作用

    @Override protected void onCreate(Bundle savedInstanceState) {  try{   super.onCreate(savedInstanceS ...

随机推荐

  1. php 连接mongodb 增查改删操作

    查询 <?php $m=new MongoClient('mongodb://admin:admin@localhost:27017/admin'); $db=$m->hndb; $cc= ...

  2. delphi Inc函数和Dec函数的用法

    inc自增函数 .inc(i,n)://i,n:integer;n为自增量 相当于i:=i+n: .inc(i)://i:integer; 相当于i:=i+;   dec自减函数 .dec(i,n): ...

  3. mysql数据类型最大长度记录

    MySQL中各数据类型的取值范围 转在这里,慢慢记下来. TINYINT -128 - 127 TINYINT UNSIGNED 0 - 255 SMALLINT -32768 - 32767 SMA ...

  4. webform注册和Repeater

    一.注册1.日期(1)年月日用三DropDownList个,分别循环写入数字 代码写在后台  Page_Load中的代码 if (IsPostBack == false) { //年绑定数据 ; i- ...

  5. Nginx配置配置文件nginx.conf的设置

    引用自:http://www.ha97.com/5194.html #定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_pro ...

  6. Java关键字总结及详解

    Java关键字是Java的保留字,这些保留字不能用来作为常量.变量.类名.方法名及其他一切标识符的名称. 一.基本数据类型 Java中有八种基本数据类型,六种数字类型(四个整数型.六中浮点型),一种字 ...

  7. Java核心知识点学习----线程中的Semaphore学习,公共厕所排队策略

    1.什么是Semaphore? A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each acq ...

  8. ios8以后,使用UIAlertViw时pop/push页面后,键盘闪一下的问题

    代码为 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"感谢你对我们提出的意见或 ...

  9. Android学习之 Intent详解

    一. Intent 作用 Intent 是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯.比如说调用startActivity()来 ...

  10. 去掉UItableview headerview黏性(sticky)

    // 去掉UItableview headerview黏性(sticky) - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFlo ...