是否可以允许片段旋转而不是父片段 activity?

Is it possible to allow a fragment to rotate and not the parent activity?

我有一个 Activity 和一个全屏片段,它继承自 AppCompatDialogFragment。在 Activity 上创建静态实例并调用 Show 方法启动它。

我希望片段能够旋转但 Activity。

我尝试在 OnCreate 的覆盖中将 activity 的请求方向设置为 'Portrait',并在片段的 OnCreate 中再次将其设置为 'Unspecified'。问题是当屏幕旋转时,Activity 被重新创建,片段的 OnCreate 在 Activity.w

的 OnCreate 之前被调用

感谢任何帮助。

I have an Activity and a fullscreen fragment, which inherits from AppCompatDialogFragment. A static instance is created on the Activity and the Show method is called to launch it.

I'd like the fragment to be able to rotate but not the Activity.

只需为片段的旋转赋值即可:

public class MainActivity : AppCompatActivity
{
    AppCompatDialogFragment testFragment;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        testFragment = new TestFragment();
        testFragment.Show(SupportFragmentManager, "abc");
    }
 }

并且在Fragment中我设置了一个按钮,点击时我将Fragment的Rotation设置为增加5:

public class TestFragment : AppCompatDialogFragment
{
    Button btnClick;
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);

       View view=inflater.Inflate(Resource.Layout.test_fragment, container, false);
        btnClick = view.FindViewById<Button>(Resource.Id.btnClick);
        btnClick.Click += BtnClick_Click;
        return view;
    }

    private void BtnClick_Click(object sender, EventArgs e)
    {
        this.View.Rotation += 5f;
    }
}

注意:您必须管理片段的大小,以防视图在旋转后超出边界。