いい質問タイトルが思いつきませんでした。申し訳ありません。
ここに私の問題があります: 私は私の主な活動を持っています:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V4.App;
namespace AndroidApplication2
{
[Activity(Label = "AndroidApplication2", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : FragmentActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//Not sure if I need the code below
FragmentTransaction ft = this.SupportFragmentManager.BeginTransaction();
Activity2 fragment = new Activity2();
ft.Add(Resource.Id.fragmentContainer, fragment);
ft.Commit();
}
}
}
メイン アクティビティの XML ファイルは次のようになります。
<?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">
<fragment
class="AndroidApplication2.Activity2"
android:id="@+id/fragmentContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/changeFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Fragment" />
</LinearLayout>
これで、Activity2 は次のようになります。
namespace AndroidApplication2
{
[Activity(Label = "My Activity")]
public class Activity2 : Fragment
{
public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2)
{
return p0.Inflate(Resource.Layout.fragment_layout, p1, false);
}
}
}
fragment_layout.axml は次のようになります。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView from the fragment" />
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a Button from the fragment" />
</LinearLayout>
このコードは、Xamarin を使用した C# です。
ここでやりたいことは、fragment_layout のボタンにアクセスし、それを使用して Activity1 (メイン アクティビティ) のフラグメントを置き換えることだけです。何かの詳細を表示するなど、単純なことごとに新しいアクティビティを作成しないようにするためにこれを使用していますが、ポップアップも使用できません。
メインの FragmentActivity にロードしたフラグメントのボタンにアクセスできません。別のフラグメントに置き換えることができます。ヘルプ!ありがとう!その他の改善や提案も大歓迎です。