[Xamrin]Fragment静態編を簡単に使います.

11588 ワード

新たなAndroidの開発は、Fragmentには非常によく使われていますが、公式の例はちょっと複雑で、初心者には消化しにくいので、心得をメモしておきます.ここのブログは静的な方法でフラジャットを使います.前に記事で述べました.  Layout Inflater.Inflateを使ってあらかじめ設計されたLayoutにロードして使用します。 一つの画面を元気づけるということですが、メインのMainActivityにおいて呼び出しされるラyout素子を制御しなければなりません.Android 3.0以降、多くの開発素子のメーカーが改版し、Fragmnetで包装しています.彼はスクリーン解像度についての議題が良い解決方法があります.もちろんこれはこの篇の重点ではありません.今日の判例を見に来ました.
私はラyoutの名前をFragment 1として設計しました.MainActivityからDisplayContextを愈してほしいです.そしてFragmentのボタンを押したらTextViewの上に表示されます.
この私が設計した部品は、主要なActivityでプレビューを読み込むことができることを望んでいます.
1.まず、これはAndroid 3.0で加入したものですので、基本的に2.xは他の方法でここでは取り上げません.重点は先に特別案件を4.0以上のバージョンに調整しましょう.
2.素子として利用されるLayoutを作成します.このモデルは「Resource\Layout\Fragment 1.axmlです.
<?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="  Fragment" />
    <Button
        android:text="Fragment  "
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnChangeText" />
</LinearLayout>
3.次はこのプログラムに魂を与えて、このLayoutを操作します.  ファイルの位置は\Fragment 1.csです.
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
 
namespace StaticFragment1
{
    public class Fragment1 : 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.Fragment1, 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.主なMainActivity Layout  Resource\Layout\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    Fragment" />
    <fragment
        android:id="@+id/fragmentFirst"
        android:name="StaticFragment1.Fragment1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
その中で注意してください.fragmentのnameは設定です.  [案件名になる].[Fragment名]packagemeフルネームを書く例が多いですが、ここでは走れないので、直接に対応するべきです.
5.続いてMainActivityの中でC菗の部分が\Activity 1.csに位置します.
using Android.App;
using Android.OS;
 
namespace StaticFragment1
{
    [Activity(Label = "    Fragment", 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);
 
            //   Fragment    Layout         Fragment1
            var fragment1 = FragmentManager.FindFragmentById<Fragment1>(Resource.Id.fragmentFirst);|
            //      Peoperty
            fragment1.DisplayContext = "Test    ";
 
 
        }
    }
}
 
結果:実行します.
を選択します
クリックして-
ちょっと複雑なので、簡単なバージョンを整理します.この方法はシングルパスより画面inflateを管理しやすいです.Reference:http://docs.xamarin.com/guides/android/platform_feat ures/fragments.
http://blog.kenyang.net/2013/03/android-fragment-activity.html