简体   繁体   English

Xamarin Android Align小部件工具栏图标向右

[英]Xamarin Android Align widget toolbar icon to right

I am following a tutorial to make a side menu from here https://www.c-sharpcorner.com/article/xamarin-android-create-left-drawer-layout 我正在按照教程从此处制作侧面菜单https://www.c-sharpcorner.com/article/xamarin-android-create-left-drawer-layout

Currently the menu icon button is aligned to the left but i want to align it to the right. 目前,菜单图标按钮向左对齐,但我想将其向右对齐。 I have no idea how to chage the icons alignment. 我不知道如何改变图标的​​对齐方式。

in my mainActivity 在我的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);
    }

and then in menu.axml that i load as the content view 然后在我加载为内容视图的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>

Use the following code to make changes in your toolbar. 使用以下代码在工具栏中进行更改。 This way you can customize your toolbar. 这样,您可以自定义工具栏。

Replace this: 替换为:

  <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"/>

By this: 这样:

    <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM