当用户点击操作栏中的某个项目时,我正在尝试向活动添加一个片段,但是我无法将其显示出来。这是主要的代码没有按照我期望的方式工作(这是用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>
如果我立即添加片段,这又有效,否则当我尝试稍后添加它时,活动视图永远不会改变以显示片段。几乎就像我需要告诉活动重新粉刷?任何帮助,将不胜感激。提前谢谢。
答案 0 :(得分:2)
如果我帮助其他人,我会弄清楚问题是什么。添加新片段后,我需要让Activity
重新布局。根据我的搜索,我看到关于调用invalidate的评论如下:
FindViewById<LinearLayout>(Resource.Id.plex_frame_container).Invalidate();
然而,这是不正确的(至少在我的情况下),并没有让它调整大小(或更正确地重新布局)视图。但是,调用RequestLayout
正是我所需要的,并且片段开始显示。例如:
FindViewById<LinearLayout>(Resource.Id.plex_frame_container).RequestLayout();
希望这有帮助。