片段内的布局夸大

时间:2019-05-09 21:30:03

标签: c# android-fragments xamarin.android

我有问题。我正在使用FrameLayout使用片段。现在在我的片段之一中,我想创建一个带有EditText的对话框。所以我为fragment.cs编写了这段代码:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View view = inflater.Inflate(Resource.Layout.intro_add_new_project, container, false);

    ImageButton btnAddProject = view.FindViewById<ImageButton>(Resource.Id.btnAddProject);
    btnAddProject.Click += delegate
    {
        LayoutInflater layoutinflater = LayoutInflater.From(this);
        View DialogView = layoutinflater.Inflate(Resource.Layout.setup_project_name_dialog, null);
        Android.Support.V7.App.AlertDialog.Builder ProjectNameDialog = new Android.Support.V7.App.AlertDialog.Builder(this);
        ProjectNameDialog.SetView(DialogView);

        EditText editProjectName = DialogView.FindViewById<EditText>(Resource.Id.editProjectName);
        ProjectNameDialog.SetCancelable(false);
        ProjectNameDialog.SetPositiveButton("Continue", delegate
        {
            //My own code here
        });
    };


    return view;
}

现在我遇到2x this的问题。它给出以下错误:

  

无法从“ Appname.Fragments.Intro_Add_Project”转换为   “ Android.Content.Context”

我应该填写什么而不是this。别忘了...。这是一个片段!

有人可以帮我吗?

更新

我得到的错误是:

  

{Java.Lang.IllegalStateException:您需要使用Theme.AppCompat   活动的主题(或后代)。在   Java.Interop.JniEnvironment + InstanceMethods.CallObjectMethod   (Java.Interop.JniObjectReference实例,Java.Interop.JniMethodInfo   方法,Java.Interop.JniArgumentValue * args)[0x00069]在   <42dc777b518744fdae9988e94489a4a0>:0在   Java.Interop.JniPeerMembers + JniInstanceMethods.InvokeVirtualObjectMethod   (System.String编码的成员,Java.Interop.IJavaPeerable自我,   Java.Interop.JniArgumentValue *参数)[0x0002a]在   <42dc777b518744fdae9988e94489a4a0>:0在   Android.Support.V7.App.AlertDialog + Builder.Show()[0x0000a]在   <6bc524e87f8f4e14ab823997c1f60345>:0在   Appname.Fragments.Intro_Add_Project.b__1_0   (System.Object,System.EventArgs)[0x0005b]在   C:\ Users \ Alexander \ source \ repos \ Appname \ Appname \ Fragments \ Intro_Add_Project.cs:45   ---托管Java.Lang.IllegalStateException堆栈跟踪的结尾--- java.lang.IllegalStateException:您需要使用Theme.AppCompat   活动的主题(或后代)。在   android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:354)     在   android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:323)     在   android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:284)     在   android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:83)     在   android.support.v7.app.AlertController.installContent(AlertController.java:226)     在android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:260)     在android.app.Dialog.dispatchOnCreate(Dialog.java:407)   android.app.Dialog.show(Dialog.java:302)位于   android.support.v7.app.AlertDialog $ Builder.show(AlertDialog.java:958)     在mono.android.view.View_OnClickListenerImplementor.n_onClick(本机   方法)   mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:30)     在android.view.View.performClick(View.java:6669)处   android.view.View.performClickInternal(View.java:6638)在   android.view.View.access $ 3100(View.java:789)在   android.view.View $ PerformClick.run(View.java:26145)在   android.os.Handler.handleCallback(Handler.java:873)在   android.os.Handler.dispatchMessage(Handler.java:99)位于   android.os.Looper.loop(Looper.java:193)在   android.app.ActivityThread.main(ActivityThread.java:6863)位于   java.lang.reflect.Method.invoke(本机方法)位于   com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:537)     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)}

2 个答案:

答案 0 :(得分:2)

您应该LayoutInflater layoutinflater = LayoutInflater.From(Activity);

Android.Support.V7.App.AlertDialog.Builder ProjectNameDialog = new Android.Support.V7.App.AlertDialog.Builder(Activity);

这是我的片段代码。(不要忘记添加ProjectNameDialog.Show();会弹出AlertDialog)

     public class Fragment1 : Fragment
{
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.Inflate(Resource.Layout.activity_main, container, false);

        ImageButton btnAddProject = view.FindViewById<ImageButton>(Resource.Id.btnAddProject);
        btnAddProject.Click += delegate
        {
            LayoutInflater layoutinflater = LayoutInflater.From(Activity);
            View DialogView = layoutinflater.Inflate(Resource.Layout.setup_project_name_dialog, null);
            Android.Support.V7.App.AlertDialog.Builder ProjectNameDialog = new Android.Support.V7.App.AlertDialog.Builder(Activity);
            ProjectNameDialog.SetView(DialogView);

            EditText editProjectName = DialogView.FindViewById<EditText>(Resource.Id.et_name);
            ProjectNameDialog.SetCancelable(false);
            ProjectNameDialog.SetPositiveButton("Continue", delegate
            {
                //My own code here
            });

            ProjectNameDialog.Show();
        };
        return view;
    }
}

有MainActivity.cs

    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.main);
       var Fragment1 = new Fragment1();
       FragmentManager.BeginTransaction()
                    .Add(Android.Resource.Id.Content, Fragment1)
                    .Commit();
    }


}

正在运行GIF

enter image description here

答案 1 :(得分:0)

根据Xamarin Online Documentation LayoutInflater.From 方法需要一个 Android.Content.Context 对象。因此,假设您的片段已从 Android.App.Fragment 继承,正如Xamarin docs sample所建议的那样,是针对您的片段而不是您的应用程序上下文。

LayoutInflater layoutinflater = LayoutInflater.From(this);