简体   繁体   English

如何在膨胀视图工具栏中显示工具栏图标 android java

[英]How to display toolbar icons inside an inflated view toolbar android java

I have an inflated view in my activity file and the layout file happens to have a Toolbar widget that I desire to fill up with icons.我的活动文件中有一个膨胀的视图,而布局文件恰好有一个我希望用图标填充的工具栏小部件。 I have tried to use the code toolbar.Inflate(int resource);我曾尝试使用代码toolbar.Inflate(int resource); but it is not showing up, the toolbar text is also not showing up with toolbar.Title=name;但它没有显示出来,工具栏文本也没有显示为toolbar.Title=name; The code for the layout is this one...布局的代码是这个......

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/chat_background"
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar3"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

     </com.google.android.material.appbar.AppBarLayout>
    <EditText
        android:layout_margin="5dp"
        android:layout_alignParentBottom="true"
        android:padding="9dp"
        android:drawableRight="@drawable/tinted_camera"
        android:background="@drawable/rounded"
        android:drawableLeft="@drawable/emoji"
        android:hint="Type a Message"
        android:inputType="textMultiLine"
        android:id="@+id/message_box"
        android:gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>

</RelativeLayout>

I tried to inflate the View first, got the ID of my toolbar and tried to inflate the icons to the right of the toolbar as follows but its not working.我尝试先膨胀视图,获取工具栏的 ID,然后尝试按如下方式膨胀工具栏右侧的图标,但它不起作用。

            try
            {
                //Inflate our view with the Chat layout
                View chat_view = LayoutInflater.Inflate(Resource.Layout.chat_view, null);
                //define the toolbar
                AndroidX.AppCompat.Widget.Toolbar toolbar = chat_view.FindViewById<AndroidX.AppCompat.Widget.Toolbar>(Resource.Id.toolbar3);
                SetSupportActionBar(toolbar);
                //get the nanme of the current contact
                string name = (string)contact_list.GetItemAtPosition(e.Position);
                toolbar.Title= name;
                //inflate this toolbar
                toolbar.InflateMenu(Resource.Menu.video_menu_item);
                toolbar.InflateMenu(Resource.Menu.call_menu_item);
                toolbar.InflateMenu(Resource.Menu.bars_menu_item);
                toolbar.SetNavigationIcon(Resource.Drawable.back_arrow);
                //build a new alert dialog
                Android.App.AlertDialog.Builder builder = new Android.App.AlertDialog.Builder(this, Android.Resource.Style.ThemeMaterialLightNoActionBarFullscreen);
                builder.SetView(Resource.Layout.chat_view);
                builder.Show();
            }catch (Exception ex)
            {
                Toast.MakeText(this,ex.Message,ToastLength.Short).Show();
            }

How do I succeSsfully make this Toolbar in an inflated view to show up the icons and the text?我如何成功地使这个工具栏在一个膨胀的视图中显示图标和文本?

Try this:尝试这个:

<androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar3"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:menu="@menu/your_menu"
        app:navigationIcon="@drawable/your_icon" />

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/new_game"
          android:icon="@drawable/ic_new_game"
          android:title="@string/new_game"
          android:showAsAction="ifRoom"/>
    <item android:id="@+id/help"
          android:icon="@drawable/ic_help"
          android:title="@string/help" />
</menu>

I think you need to check your menu.xml file.我认为你需要检查你的 menu.xml 文件。

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

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