在活动的工具栏中设置应用程序图标,右侧 [英] set app icon to right side in activity tool bar

查看:147
本文介绍了在活动的工具栏中设置应用程序图标,右侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作与AppCompact'库和面临的一些问题与布局/定位。我想在动作条的右侧放置应用程序图标。一种方法是定义工具栏中的按钮,但有一种标准的方法来设置应用程序图标,上​​按钮的动作条?

的右手边

正如你可以在上面的图片看到,该图标放置在左边,我想这是在正确的。任何帮助将是AP preciated。

诗:对于谁可能面临我的问题,它可以很容易地使用这个code是固定的人 添加该code来体现:

 <应用的Andr​​oid版本:supportsRtl =真正的>
 

,然后写在OnCreate这个code:

  getWindow()getDecorView()setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
 

解决方案

有没有办法Android提供了设置应用程序图标,在右侧的动作条的,但你仍然可以做到这一点

创建一个菜单,说 main_menu.xml

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto>

    <项目机器人:ID =@ + ID / MENU_ITEM
        机器人:图标=@可绘制/ your_app_icon
        机器人:标题=@字符串/ MENU_ITEM
        应用程序:showAsAction =总是/> //设置showAsAction总是
                                    //这应该是与展示作为动作总是唯一的菜单项

< /菜单>
 

现在只是替换 onCreateOptionsMenu 在你的类文件。

MainActivity.class

添加此

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){

    。getMenuInflater()膨胀(R.menu.main_menu,菜单);
    返回super.onCreateOptionsMenu(菜单);
}
 

它的完成!您的应用程序图标会在右边动作条现在可见的。

如果您的菜单中的多个项目以及替换 在prepareOptionsMenu 的.class文件并设置的setEnabled(假)具有菜单项的应用程序图标,这样做prevents你的图标点击。

  @覆盖
公共布尔prepareOptionsMenu(功能菜单)在{
    menu.findItem(R.id.menu_item).setEnabled(假);

    返回super.on prepareOptionsMenu(菜单);
}
 

现在你的 MainActivity.class 文件看起来像

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){

    。getMenuInflater()膨胀(R.menu.main_menu,菜单);
    返回super.onCreateOptionsMenu(菜单);
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){

    开关(item.getItemId()){
        案例R.id.menu_item://这个项目有你的应用程序图标
            返回true;

        案例R.id.menu_item2://其他菜单项,如果您有任何
            //在此处添加任何行动
            返回true;

        案例... //做其他所有菜单项

        默认值:返回super.onOptionsItemSelected(项目);
    }
}

@覆盖
公共布尔prepareOptionsMenu(功能菜单)在{
    menu.findItem(R.id.menu_item).setEnabled(假);

    返回super.on prepareOptionsMenu(菜单);
}
 

这是在欺骗您可以使用右侧设置应用程序图标。

I am working with the 'AppCompact' library and faced some problems with layout/positioning. I want to position the App icon on the right hand side of the 'ActionBar'. One method is to define a button in the toolbar, but is there a standard method to set the App icon and Up button on the right hand side of the ActionBar?

As you can see in the above image, the icon is positioned on the left, I want it to be on the right. Any help would be appreciated.

P.s:For people who may face my problem it can be fixed easily using this code Add this code to manifest:

<application android:supportsRtl="true">

and then write this code on Oncreate:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

解决方案

There is no way android provides to set app icon at right side of actionbar but you can still do that.

Create a menu, say main_menu.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/menu_item"
        android:icon="@drawable/your_app_icon"
        android:title="@string/menu_item"
        app:showAsAction="always"/>  //set showAsAction always
                                    //and this should be the only menu item with show as action always

</menu>

Now just override onCreateOptionsMenu in your class file.

add this in MainActivity.class

@Override
public boolean onCreateOptionsMenu(Menu menu){

    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

It's done! Your app icon will be visible on right side of ActionBar now.

If you have more than one item in menu then override onPrepareOptionsMenu in .class file and set setEnabled(false) for menu item having app icon, doing this prevents your icon to be clickable.

@Override
public boolean onPrepareOptionsMenu(Menu menu){
    menu.findItem(R.id.menu_item).setEnabled(false);

    return super.onPrepareOptionsMenu(menu);
}

Now your MainActivity.class file would look like

@Override
public boolean onCreateOptionsMenu(Menu menu){

    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item){

    switch(item.getItemId()){
        case R.id.menu_item:   //this item has your app icon
            return true;

        case R.id.menu_item2:  //other menu items if you have any
            //add any action here
            return true;

        case ... //do for all other menu items

        default: return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onPrepareOptionsMenu(Menu menu){
    menu.findItem(R.id.menu_item).setEnabled(false);

    return super.onPrepareOptionsMenu(menu);
}

This is the only trick you can use to set app icon on right side.

这篇关于在活动的工具栏中设置应用程序图标,右侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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