android - Xamarin Android 菜单图标不显示

标签 android xml xamarin menu icons

你能帮我吗?我看不到任何图标。这是由于我的图标大小或我的 xamarin 配置还是真的是代码?我尝试将 xamarin 工具栏指南一对一复制到我的代码中,但没有任何改变。不幸的是,作为初学者,我无法发布屏幕图像:/。右上角应该有一个“X”图标用于取消或至少有三个点,但没有显示任何内容。

代码:

Main.axml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
    <Space
        android:id="@+id/s0"



android:layout_below="@id/toolbar"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_margin="5dp" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button1"
    android:layout_below="@id/s0"
    android:layout_height="20dp"
    android:drawablePadding="10dip"
    android:textSize="12sp"
    android:gravity="start|center_vertical"
    android:layout_width="fill_parent"
    android:text="@string/rate"
    android:drawableLeft="@drawable/ic_action_star_icon"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v1"
    android:layout_below="@id/button1"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button2"
    android:layout_below="@id/v1"
    android:textSize="12sp"
    android:drawablePadding="10dip"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/empfehlen"
    android:drawableLeft="@drawable/ic_action_google_plus_one"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v2"
    android:layout_below="@id/button2"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button3"
    android:layout_below="@id/v2"
    android:textSize="12sp"
    android:drawablePadding="10dip"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/gefaellt"
    android:drawableLeft="@drawable/ic_action_like"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v3"
    android:layout_below="@id/button3"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button4"
    android:drawablePadding="10dip"
    android:layout_below="@id/v3"
    android:textSize="12sp"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/feedback"
    android:drawableLeft="@drawable/ic_action_unnamed"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v4"
    android:layout_below="@id/button4"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button5"
    android:layout_below="@id/v4"
    android:drawablePadding="10dip"
    android:textSize="12sp"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:paddingLeft="22dip"
    android:text="@string/impr"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v5"
    android:layout_below="@id/button5"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button6"
    android:layout_below="@id/v5"
    android:drawablePadding="10dip"
    android:textSize="12sp"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:paddingLeft="22dip"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/schutz"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v6"
    android:layout_below="@id/button6"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Button
    android:layout_margin="10dp"
    android:id="@+id/button7"
    android:layout_below="@id/v6"
    android:drawablePadding="10dip"
    android:textSize="12sp"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:paddingLeft="22dip"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/nutz"
    android:background="@android:color/transparent" />
<View
    android:layout_margin="10dp"
    android:id="@+id/v7"
    android:layout_below="@id/button7"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="#d3d3d3" />
<Space
    android:id="@+id/s1"
    android:layout_below="@id/v7"
    android:layout_width="fill_parent"
    android:layout_height="10dip" />
<Button
    android:id="@+id/button8"
    android:layout_below="@id/s1"
    android:textSize="12sp"
    android:layout_height="20dp"
    android:layout_width="fill_parent"
    android:text="@string/ab"
    android:background="@android:color/transparent"
    android:textColor="@color/red"
    android:gravity="start|center_vertical"
    android:layout_alignParentLeft="true"
    android:paddingLeft="22dp"
    android:layout_margin="10dp" />

</RelativeLayout>

top_menus.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:local="http://schemas.android.com/apk/res-auto">
      <item
           android:id="@+id/x"
           android:title="prille"
           android:icon="@drawable/ic_action_star_icon"
           local:showAsAction="always" />
    </menu>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/app_bar_layout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">

  <android.support.v7.widget.Toolbar
         xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" >

    <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="More"
       android:textColor="#000000"
       android:layout_gravity="center"
       android:id="@+id/toolbar_title" />

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

MainActivitiy.cs

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using Toolbar = Android.Support.V7.Widget.Toolbar;
using static Android.Resource;
using static App2.Resource;
using System;

namespace App2
{
    [Activity(Label = "More", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

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

            //ActionBar.Title = "My Toolbar";

            SetContentView(Resource.Layout.Main);
        }

        public override bool OnCreateOptionsMenu(Android.Views.IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.top_menus, menu);
            return base.OnCreateOptionsMenu(menu);
        }
    }
}

此外这对我不起作用://ActionBar.Title = "My Toolbar"; 总是给我它指向空引用对象的消息,但工具栏肯定已设置并显示在我的应用程序中!

所以我用工具栏中的 TextView 来实现。

请帮帮我!!非常感谢

最佳答案

您不应该使用工具栏。 AppCompatActivity 已经为右上角菜单提供了合适的操作栏。也为了你的标题。

看看here获得完整的教程。

使用此代码:

MainActivity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;

namespace App1
{
    [Activity(Label = "More", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            SupportActionBar.Title = "My Toolbar";
        }

        public override bool OnCreateOptionsMenu(Android.Views.IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.top_menus, menu);
            return base.OnCreateOptionsMenu(menu);
        }
    }
}

Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/MyButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Hello" />
</LinearLayout>

top_menus.xml

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
  <item
       android:id="@+id/x"
       android:title="prille"
       android:icon="@drawable/appicon_cloche"
          app:showAsAction="always"/>
</menu>

如上所述here ,在 list 中,您必须在应用程序标记中添加以下代码:

android:theme="@style/Theme.AppCompat.Light"

这是给出的结果:

enter image description here

关于android - Xamarin Android 菜单图标不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42946723/

相关文章:

javascript - 从外部源/文件填充 Chrome 上下文菜单

c# - 在 Xamarin 中发布 Json 字符串

android - MVVMCross PictureChooser 插件在 Android 上(不再)工作

android - 我应该对单例使用改造吗?

java - 有官方的 MRSS xsd 吗?

javascript - React Native AsyncStorage 调用无效,不进入回调或 .then

ruby - Savon 将 XML 作为字符串返回,而不是散列

c# - Newtonsoft.JSON serializeobject 返回空 JSON 字符串

java - 不使用否定按钮关闭警报对话框

android - 将 ARM NEON 代码移植到 AARCH64,很多问题