微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自定义工具栏、菜单图标未显示 Xamarin Android

如何解决自定义工具栏、菜单图标未显示 Xamarin Android

我正在尝试用自定义工具栏替换 Xamarin android 项目中的操作栏。我希望我的工具栏在左侧显示一个徽标,然后在右侧显示我的菜单图标。

我密切关注 Microsoft 的本教程Replacing the Action Bar

但是我的菜单图标不再显示(当我使用 ActionBar 时,我的菜单图标显示得很好)。然而,有一个 3 点溢出图标,但单击它时没有任何反应。这是带有我的自定义工具栏的内置应用的屏幕截图:

enter image description here

我试过了:

  • 我以为我的图像视图挡住了菜单图标,所以我将其删除 但我的菜单图标没有显示
  • 更改 MainActivty 扩展的内容,我尝试了 Activity 和 AppCompactActivity
  • 我认为我的 OnCreateOptionsMenu 可能不会被调用,所以我尝试了:toolbar.InflateMenu(Resource.Menu.actionbar);

我被卡住了,不知道这里出了什么问题,我将不胜感激!

这是我的代码

工具栏.xml

<?xml version="1.0" encoding="utf-8"?>
<Toolbar xmlns:android ="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/actionBarSize"
    android:background="#4F703A"
    android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar">
    <RelativeLayout 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="?android:attr/actionBarSize"
        android:src="@drawable/logo_app"/>
  </RelativeLayout>
</Toolbar>

MainActivity.cs(省略了一些不相关的代码

public class MainActivity : Activity
{
    [System.Obsolete]
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this,savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        //Activate customer toolbar
        var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
        SetActionBar(toolbar);
        ActionBar.Title = "";
    }

我的菜单项 (actionbar.xml)

    <?xml version="1.0" encoding="utf-8" ?>
<!--For all properties see: https://aka.ms/android-menu-resource-->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
  <item android:id="@+id/action_search"
        app:showAsAction="always"
        android:text="Search"
        android:icon="@android:drawable/ic_menu_search"/>
  <item android:id="@+id/action_addnew"
        app:showAsAction="always"
        android:text="Search"
        android:icon="@android:drawable/ic_menu_add"/>     
</menu>

style.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:colorPrimary">#5A8622</item>
  </style>
</resources>

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/frameLayoutParent"
        android:background="#4F703A">
        <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>
    <android.support.v4.widget.SwipeRefreshLayout 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:fillViewport="true"
        android:id="@+id/swipeLayout"
        android:paddingTop="?android:attr/actionBarSize">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/headerLinearLayout">
            <TextView
                android:text="Thumbnail"
                android:id="@+id/headerThumb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=".01"
                android:gravity="center_horizontal"
                android:textSize="16sp"/>
            <TextView
                android:text="Address Line"
                android:id="@+id/headerAddressLine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=".01"
                android:gravity="center_horizontal"
                android:textSize="16sp"/>
            <TextView
                android:text="City"
                android:id="@+id/headerCity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=".01"
                android:gravity="center_horizontal"
                android:textSize="16sp"/>
            <TextView
                android:text="State"
                android:id="@+id/headerState"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=".01"
                android:gravity="center_horizontal"
                android:textSize="16sp"/>
            <TextView
                android:text="Zip Code"
                android:id="@+id/headerZipCode"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=".01"
                android:gravity="center_horizontal"
                android:textSize="16sp"/>
        </LinearLayout>
        <ListView
                android:paddingTop="5dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/myListView"/>
        </LinearLayout>

    </android.support.v4.widget.SwipeRefreshLayout>
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editSearch"
            android:hint="Search"
            android:textColor="#000"
            android:editable="true"
            android:paddingTop="?android:attr/actionBarSize"/>

</FrameLayout>

解决方法

TitleView 听起来像是您尝试做的事情的最佳选择。而不是那些漫长的过程,你可以这样做

<NavigationPage.TitleView>
    <StackLayout Orientation="Horizontal">
       <Image Source="Logo.png"/>
       <label Text="Company Name"/>
    </StackLayout>
</NavigationPage.TitleView>
,

我的问题在我的菜单 xml 文件中,xmlns:app 用于 showAsAction 属性

app:showAsAction="always"

需要 v7 支持工具栏,但不用于新的棒棒糖工具栏。所以它应该是这样的:

android:showAsAction="ifRoom"

修复此问题后,我无法单击菜单项。我发现我的布局显示在顶部,所以我需要添加

 android:layout_marginTop="?android:attr/actionBarSize"

到我的自定义工具栏下方的布局!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。