Xamarin Android Align小工具栏图标右侧

时间:2018-05-10 09:25:25

标签: c# android xml xamarin

我正在按照教程从这里制作侧面菜单 https://www.c-sharpcorner.com/article/xamarin-android-create-left-drawer-layout

目前菜单图标按钮与左侧对齐,但我想将其对齐。我不知道如何使图标对齐。

在我的mainActivity中

        protected override void OnCreate(Bundle bundle)
    {

        base.OnCreate(bundle);
        SetContentView(Resource.Layout.menu);

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

        SupportActionBar.SetDisplayHomeAsUpEnabled(true);
        SupportActionBar.SetDisplayShowTitleEnabled(true);
        SupportActionBar.SetHomeButtonEnabled(true);
        SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.menu_icon);


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


    }

        public override bool OnOptionsItemSelected(IMenuItem item)
    {
        switch (item.ItemId)
        {
            case Android.Resource.Id.Home:
                drawerLayout.OpenDrawer(Android.Support.V4.View.GravityCompat.End);
                return true;
        }
        return base.OnOptionsItemSelected(item);
    }

然后在我加载为内容视图的menu.axml中

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="65.5dp"
        android:minHeight="?attr/actionBarSize"
        android:background="#ff356f4d"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways"
        app:title="Overview - House 1"
        app:titleTextColor="#ffffff"
        android:layout_marginLeft="0.0dp"
        android:layout_marginBottom="0.0dp" 
  android:layout_gravity="right"/>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        >
        <RelativeLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_height="match_parent"
            android:layout_width="200dp"
            android:layout_gravity="right"
            android:fitsSystemWindows="true"
            app:menu="@menu/menu" />
</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:2)

使用以下代码在工具栏中进行更改。这样您就可以自定义工具栏。

替换它:

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="65.5dp"
    android:minHeight="?attr/actionBarSize"
    android:background="#ff356f4d"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:layout_scrollFlags="scroll|enterAlways"
    app:title="Overview - House 1"
    app:titleTextColor="#ffffff"
    android:layout_marginLeft="0.0dp"
    android:layout_marginBottom="0.0dp" 
    android:layout_gravity="right"/>

由此:

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="#33B86C"
    app:layout_scrollFlags="scroll|enterAlways">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <TextView
                android:id="@+id/toolbar_tv_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:maxLines="1"
                android:textColor="#FFFFFF"
                android:textAllCaps="true"
                android:textSize="18sp"
                android:text="Home"/>

            <ImageView
                android:id="@+id/toolbar_iv_right"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="end"
                android:layout_marginEnd="10dp"
                android:contentDescription="@null"
                android:src="@drawable/menu_icon" />

        </FrameLayout>

    </android.support.v7.widget.Toolbar>