如何在android中添加图标到actionbar [英] how to add icon to actionbar in android

查看:554
本文介绍了如何在android中添加图标到actionbar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在操作栏中添加一个图标,我的代码不会在操作栏中显示图标。它显示的是一个带有三个点的图标,当我点击它时,它通过点击它显示回来执行我指定的操作。这是我的以下代码



menu1.xml

< item 
android:id = @ + id / action_back
android:orderInCategory = 100
android:showAsAction = 始终
android:icon = @ drawable / back
android:title = 返回 />





活动

  @覆盖 
public < span class =code-keyword> boolean onCreateOptionsMenu(菜单菜单) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu1,menu);
返回 true;
}
@覆盖
public boolean onOptionsItemSelected(MenuItem item){
if (item.getItemId()== R.id.action_back){
Toast.makeText(getApplicationContext(), Teritary,Toast.LENGTH_SHORT).show( );
}


return true;
}

解决方案

android:orderInCategory 属性指示菜单项在显示时在菜单中出现的顺序。菜单项按orderInCategory整数值的升序从左到右排列(即1,2,3 ... - >从左到右)。



android:orderInCategory 实际上有两种用途。

=>对于ActionBar中的菜单项。根据升序,项目将在ActionBar中从左到右显示。



=>对于溢出菜单中的菜单项。溢出菜单项将从上到下显示,具体取决于您指定的升序。



所以,(只是一个疯狂的猜测)使它 android:orderInCategory =1,你很高兴。



-KR


< blockquote>



我相信现在的问题是兼容性。



在你的 menu.xml 文件,创建一个新的 XML命名空间



这变为:

 <  菜单    xmlns:android   =  http://schemas.android.com/apk/res/android\" 

xmlns:mynamespace = http://schemas.android.com/apk/res-auto >

< span class =code-keyword>< item

android:id = @ + id / action_back
< span class =code-attribute>
android:orderInCategory = 100

android:icon < span class =code-keyword> = @ drawable / back

android:title = 返回

android:showAsAction = 始终

mynamespace:sh​​owAsAction = 始终 / >
< / menu >





Java 源代码可保持不变。


i need to add an icon to the action bar and my code does not show icon in the action bar. what it shows is an icon with three dots and when i click on it it shows back by clicking it performs my specified operation.here is my following code

menu1.xml

<item
        android:id="@+id/action_back"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/back"
        android:title="Back"/>



activity

@Override
     public boolean onCreateOptionsMenu(Menu menu) {
       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.menu1, menu);
       return true;
     }
    @Override
     public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==R.id.action_back){
            Toast.makeText(getApplicationContext(), "Teritary", Toast.LENGTH_SHORT).show();
        }


       return true;
     }

解决方案

The android:orderInCategory attribute dictates the order in which the menu items will appear within the menu when it is displayed. The menu items are arranged from left to right in ascending order of integer value of orderInCategory (i.e. 1,2,3... -> left to right).

android:orderInCategory is actually useful in two ways.
=> For menu items in ActionBar. Items will appear from left to right in ActionBar depending on the ascending order.

=> For menu items in overflow menu. Overflow menu items will be displayed from top to bottom depending upon the ascending order you have specified.

So, (Just a wild guess) make it android:orderInCategory="1", and you are good to go.

-KR


Hi,

I believe now the problem is compatibility.

In your menu.xml file, create a new XML Namespace.

This becomes:

<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:mynamespace="http://schemas.android.com/apk/res-auto">

    <item

        android:id="@+id/action_back"

        android:orderInCategory="100"

        android:icon="@drawable/back"

        android:title="Back"

        android:showAsAction="always"

        mynamespace:showAsAction="always" />
</menu>



The Java Source code could remain as it is.


这篇关于如何在android中添加图标到actionbar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆