【问题标题】:Why is action icon not showing on actionbar为什么操作栏上没有显示操作图标
【发布时间】:2018-09-14 13:21:30
【问题描述】:

我开发 android 应用程序,我想创建放置在作为图标可见的操作栏上的操作。我添加了一个分辨率为 18x18px 的可绘制对象,并创建了一个包含一项的菜单,这是我的操作,如下所示:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".RelationsActivity">

<item
    android:id="@+id/action_show_profile"
    android:icon="@drawable/profile_icon_16"
    android:title="@string/action_show_profile"
    app:showAsAction="always" />

如您所见,showAsAction 属性设置为“始终”,但仍无法正常工作。我尝试了不同的 AppThemes,但没有任何帮助。图标肯定有足够的空间,因为操作栏标题已禁用:

getActionBar().setDisplayShowTitleEnabled(false);

该操作出现在溢出下拉列表中,就好像图标没有空间一样。

【问题讨论】:

    标签: android android-actionbar icons overflow action


    【解决方案1】:

    使用这个

    Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
    

    还有这个方法

     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.YOUR_MENU_FILE, menu);
        return true;
    }
    

    【讨论】:

    • 我的活动扩展了 Activity 类而不是 AppCompatActivity,所以我不能使用 getSupportActtionBar() 函数,但我尝试使用 getActionBar() 但它没有用。
    【解决方案2】:
    implements AppCompatCallback
    
    and use this code inside oncreate method
    
      //let's create the delegate, passing the activity at both arguments (Activity, AppCompatCallback)
            AppCompatDelegate  delegate = AppCompatDelegate.create(this, this);
    
            //we need to call the onCreate() of the AppCompatDelegate
            delegate.onCreate(savedInstanceState);
    
            //we use the delegate to inflate the layout
            //delegate.setContentView(R.layout.activity_sync_contact);
    
    
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar.setNavigationIcon(R.drawable.ic_menu);// icon of navigation 
            toolbar.setTitle("Inbox");
            delegate.setSupportActionBar(toolbar);
    

    【讨论】:

    • 如果使用此代码有任何问题,请告诉我。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 2016-01-12
    相关资源
    最近更新 更多