1

当用户单击操作栏中的项目时,我试图将片段添加到活动中,但是我无法显示它。这是没有按我预期的方式工作的主要代码(这是用 Monodroid 编写的):

public override bool OnOptionsItemSelected(IMenuItem item)
    {
      if (item.ItemId == Resource.Id.show_hide_notes)
      {
        notesLayout.Visibility = ViewStates.Visible;

        notesWebViewFragment = new NotesWebViewFragment();
        // linearLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent, 1);

        Android.Support.V4.App.FragmentTransaction fragmentTransaction = SupportFragmentManager.BeginTransaction();
        fragmentTransaction.Add(Resource.Id.notes_fragment_container, notesWebViewFragment);
        fragmentTransaction.Commit();

        // Adding this line doesn't help
        // FindViewById<LinearLayout>(Resource.Id.plex_frame_container).Invalidate();
      }
      return true;
    }

当我在 OnCreate 方法中将此片段与另一个片段添加时,我得到了我所期望的。我已经尝试了几种不同的方法来尝试让它显示出来。任何解释我做错了什么都将不胜感激。我在下面添加了其余的相关代码。

 [Activity]
  public class BrainViewActivity : Android.Support.V4.App.FragmentActivity
  {
    private Brain activeBrain;

    PlexWebViewFragment plexWebViewFragment;
    NotesWebViewFragment notesWebViewFragment;

    FrameLayout notesLayout;

    protected override void OnCreate(Bundle savedInstanceState)
    {
      base.OnCreate(savedInstanceState);

      SetContentView(Resource.Layout.brain_view);

      notesLayout = FindViewById<FrameLayout>(Resource.Id.notes_fragment_container);
      // notesLinearLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent, 0);
      notesLayout.Visibility = ViewStates.Gone;

      if (savedInstanceState == null)
      {
        // Create an instance of PlexWebViewFragment
        plexWebViewFragment = new PlexWebViewFragment();
      }

      // Check that the activity is using the layout version with
      // the fragment_container FrameLayout
      if (FindViewById(Resource.Id.plex_fragment_container) != null)
      {
        plexWebViewFragment.Arguments = Intent.Extras;

        Android.Support.V4.App.FragmentTransaction fragmentTransaction = SupportFragmentManager.BeginTransaction();
        fragmentTransaction.Add(Resource.Id.plex_fragment_container, plexWebViewFragment);

        fragmentTransaction.Commit();
      }
    }

    public override bool OnCreateOptionsMenu(IMenu menu)
    {
      base.OnCreateOptionsMenu(menu);
      MenuInflater.Inflate(Resource.Menu.brain_view_menu, menu);

      return true;
    }

    public override bool OnOptionsItemSelected(IMenuItem item)
    {
      if (item.ItemId == Resource.Id.show_hide_notes)
      {
        notesLayout.Visibility = ViewStates.Visible;

        notesWebViewFragment = new NotesWebViewFragment();
        // linearLayout.LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent, 1);

        Android.Support.V4.App.FragmentTransaction fragmentTransaction = SupportFragmentManager.BeginTransaction();
        fragmentTransaction.Add(Resource.Id.notes_fragment_container, notesWebViewFragment);
        fragmentTransaction.Commit();

        // Adding this line doesn't help
        // FindViewById<LinearLayout>(Resource.Id.plex_frame_container).Invalidate();
      }
      return true;
    }
  }

xml 视图:

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/plex_frame_container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <FrameLayout
    android:id="@+id/plex_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

  <FrameLayout
    android:id="@+id/notes_fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>

如果我立即添加片段,这同样有效,否则当我稍后尝试添加它时,活动的视图永远不会改变以显示片段。几乎就像我需要告诉活动重新绘制?任何帮助,将不胜感激。提前致谢。

4

1 回答 1

2

万一这是我的帮助其他人,我想出了问题所在。添加新片段后,我需要Activity自行重新布局。从我搜索的内容中,我看到了关于调用 invalidate 的评论,如下所示:

FindViewById<LinearLayout>(Resource.Id.plex_frame_container).Invalidate();

但是,这是不正确的(至少在我的情况下),并且不会让它调整视图的大小(或更正确地重新布局)。但是,调用RequestLayout完全符合我的需要,并且片段开始显示。例子:

FindViewById<LinearLayout>(Resource.Id.plex_frame_container).RequestLayout();

希望这可以帮助。

于 2013-02-27T02:42:24.523 回答