簡體   English   中英

如何在膨脹視圖工具欄中顯示工具欄圖標 android java

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

我的活動文件中有一個膨脹的視圖,而布局文件恰好有一個我希望用圖標填充的工具欄小部件。 我曾嘗試使用代碼toolbar.Inflate(int resource); 但它沒有顯示出來,工具欄文本也沒有顯示為toolbar.Title=name; 布局的代碼是這個......

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

我嘗試先膨脹視圖,獲取工具欄的 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();
            }

我如何成功地使這個工具欄在一個膨脹的視圖中顯示圖標和文本?

嘗試這個:

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

我認為你需要檢查你的 menu.xml 文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM