2017-07-01 129 views
0

我在这里做错了什么。我想使用导航抽屉,然后为内容创建一些片段以显示不同的“页面”。我有导航视图,汉堡菜单等工作得很好。但我似乎无法让我的碎片出现。所以我尝试用文本视图替换我的片段,这样我就可以知道它是否显示出来,但我现在甚至无法将它显示出来。xamarin抽屉带碎片

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer_layout" 
    android:layout_height="match_parent" 
    android:layout_width="fill_parent" 
    android:fitsSystemWindows="true"> 
     <RelativeLayout 
       android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
     <include layout="@layout/toolbar" android:id="@+id/includething"/> 
       <TextView 
       android:id="@+id/label" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Sample Text "/> 
     <!--<FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
        android:layout_below="@id/includething"/>--> 
    </RelativeLayout > 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_height="match_parent" 
     android:layout_width="300dp" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header" /> 
</android.support.v4.widget.DrawerLayout> 

编辑:添加工具栏布局我包括

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/MyTheme"> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:elevation="4dp" 
     android:background="?attr/colorPrimary" /> 
</android.support.design.widget.AppBarLayout> 

在一个点上我得到了展现出来的片段,但它是在导航抽屉本身。现在我甚至无法让它出现。我究竟做错了什么?我现在注释掉了片段,只是希望让textview显示在工具栏下方。如果我能做到这一点,我相信我可以让片段工作。

编辑:在尝试板球模式之后,我能够更近一些,但现在看起来工具栏位于主要内容区域的顶部。有没有办法将其向下移动,以便内容从工具栏的底部开始?我将片段的背景变成了黄色,以便更容易在下面的图片中看到。你看不到的是实际上有文本出现在片段中,但被工具栏阻止。

enter image description here

+1

与LinearLayout中...... RelativeLayout的更换RelativeLayout的不具有取向 –

+1

切换到线性布局不会改变任何东西。我仍然看不到文字 –

+1

请显示您包含的工具栏布局,或将其删除。另外,DrawerLayout需要是顶层元素,而不是LinearLayout –

回答

1

DrawerLayout充当窗口内容的顶层容器,其允许交互式“抽屉”意见被从窗口中的一个或两个垂直边缘拉出。

你有一个LinearLayout中的顶级元素,而不是

你的工具栏也可以推文了看法。

您可以将您的解决方案与文档进行比较。 https://developer.android.com/training/implementing-navigation/nav-drawer.html

而您的工具栏不仅仅是一个工具栏,它是一个整体视图。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

最后一行推文本关闭屏幕

+0

这是密切的我想。看到我粘贴上面的图像。工具栏现在显示在主布局的顶部,它阻止了某些视图。 –

+1

您包含的布局不应该是CoordinatorLayout。只需将其设置为工具栏视图即可 –

1

它看起来像从XML您提供您的TextView是工具栏的后面。当使用RelativeLayout时,所有视图的位置需要相对于彼此或其父对象进行定义。

如果将android:layout_below="@id/includething"添加到TextView中,textview应该变为可见。

<TextView 
    android:id="@+id/label" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Sample Text " 
    android:layout_below="@id/includething"/> 
+0

我试过了,它没有工作。没有看到文字 –

1

这段代码在我的应用程序,在VS2017开发与Xamarin为我工作。希望它可以帮助你。

您的主。axml文件应该是这样的

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer_layout" 
    android:layout_height="match_parent" 
    android:layout_width="fill_parent" 
    android:fitsSystemWindows="true"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:background="@color/colorWhite" 
     android:id="@+id/ll"> 
     <include 
      layout="@layout/toolbar" /> 
    </LinearLayout> 
    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_height="match_parent" 
     android:layout_width="320dp" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/navbar" /> 
</android.support.v4.widget.DrawerLayout> 

您的MainActivity

public class MainActivity : AppCompatActivity 
{ 
    #region Menu 

    private DrawerLayout drawerLayout; 
    private NavigationView navigationView; 
    V7Toolbar toolbar; 

    #endregion Menu 

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

     SetContentView(Resource.Layout.Main); 

     #region Menu 

     drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout); 

     toolbar = FindViewById<V7Toolbar>(Resource.Id.toolbar); 
     SetSupportActionBar(toolbar); 

     var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.drawer_open, Resource.String.drawer_close); 
     drawerLayout.SetDrawerListener(drawerToggle); 
     drawerToggle.SyncState(); 

     navigationView = FindViewById<NavigationView>(Resource.Id.nav_view); 
     SetupDrawerContent(navigationView); 

     #endregion Menu 
    } 

    #region Menu 

    private void SetupDrawerContent(NavigationView navigationView) 
    { 
     navigationView.NavigationItemSelected += (sender, e) => 
     { 
      e.MenuItem.SetChecked(true); 

      FragmentTransaction ft = this.FragmentManager.BeginTransaction(); 

      switch (e.MenuItem.ItemId) 
      { 
       case Resource.Id.mn_Home: 
        toolbar.Title = "Dashboard"; 
        HomeFragment mg4 = new HomeFragment(); 
        ft.Replace(Resource.Id.ll, mg4); 
        break; 
      } 

      ft.Commit(); 
      drawerLayout.CloseDrawers(); 
     }; 
    } 

    public override bool OnCreateOptionsMenu(IMenu menu) 
    { 
     if (!IsNavigationMenuLoaded) 
     { 
      navigationView.InflateMenu(Resource.Menu.menu_main); 
      IsNavigationMenuLoaded = true; 
     } 

     return true; 
    } 

    #endregion Menu 
} 

你的片段

public class HomeFragment : 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 = LayoutInflater.From(Activity).Inflate(Resource.Layout.layout_Home, null); 
     return view; 
    } 
}