我正在使用 Xamarin (C#),在我的应用中,我有一些带有一些菜单项的导航视图。
我希望将它们全部对齐到视图的中心。
我怎样才能做到这一点?
我已尝试使用android:layout_gravity="center"
和android:gravity="center"
,但它们都无效。
main.axml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_below="@+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_content">
<EditText
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1" />
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Play" />
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp"
android:fitsSystemWindows="true"
android:id="@+id/nav_view"
app:menu="@menu/navmenu"
app:headerLayout="@layout/headerdrawerlayout" />
</android.support.v4.widget.DrawerLayout>
navmenu.axml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_item_overview"
android:title="Play"/>
<item
android:id="@+id/nav_item_notifications"
android:title="Notifications"/>
<item
android:id="@+id/nav_item_leaderboard"
android:title="Leaderboard"/>
<item
android:id="@+id/nav_item_profile"
android:title="My Profile"/>
<item
android:id="@+id/nav_item_friends"
android:title="Friends"/>
<item
android:id="@+id/nav_item_createQuestion"
android:title="Create Question"/>
</group>
<group android:id="@+id/group_settings_id"
android:checkableBehavior="single">
<item
android:title="Settings" />
<item
android:title="Log Out" />
</group>
</menu>
修改
我已将ListView添加到我的NavigationView,但我仍无法将菜单项置于中心位置。 你知道如何解决它吗?
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/MyTheme"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_below="@+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/main_content">
<EditText
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1" />
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Play" />
<Button
android:text="image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView1" />
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp"
android:fitsSystemWindows="true"
android:id="@+id/nav_view">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<include
layout="@layout/headerdrawerlayout" />
<ListView
android:id="@+id/list_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:choiceMode="singleChoice" />
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
// Initialize toolbar
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionBar.SetTitle(Resource.String.app_name);
// Attach item selected handler to navigation view
var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
var navListView = navigationView.FindViewById<ListView>(Resource.Id.list_menu);
List<string> listItems = new List<string> { "Play", "Notifications", "Leaderboard", "My Profile", "Friends", "Create Question" };
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, listItems);
navListView.Adapter = adapter;
//navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;
// Create ActionBarDrawerToggle button and add it to the toolbar
var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.app_name, Resource.String.app_name);
drawerLayout.AddDrawerListener(drawerToggle);
drawerToggle.SyncState();
// Add close button handler to navigation view
var navigationCloseButton = navigationView.FindViewById<ImageButton>(Resource.Id.closeButton);
navigationCloseButton.Click += delegate
{
drawerLayout.CloseDrawers();
};
}