Android 如何在 ActionBar 中同时显示操作的图标和标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12000562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:24:59  来源:igfitidea点击:

How to display both icon and title of action inside ActionBar?

androidandroid-layoutandroid-actionbaractionbarsherlockandroid-ui

提问by Alexey Zakharov

I need to display both iconand titleof action inside ActionBar.

我需要在里面同时显示icontitle操作ActionBar

I've tried "withText" option, but it has no effect.

我试过“ withText”选项,但没有效果。

enter image description here

在此处输入图片说明

回答by Yibo Long

'always|withText' will work if there is sufficient room, otherwise it will only place icon. You can test it on your phone with rotation.

如果有足够的空间,' always|withText' 将起作用,否则它只会放置图标。您可以通过旋转在手机上进行测试。

<item android:id="@id/menu_item"
android:title="text"
android:icon="@drawable/drawable_resource_name"
android:showAsAction="always|withText" />

回答by Benito Bertoli

You can create actions with text in 2 ways:

您可以通过两种方式使用文本创建操作:

1- From XML:

1-来自 XML:

<item android:id="@id/resource_name"
      android:title="text"
      android:icon="@drawable/drawable_resource_name"
      android:showAsAction="withText" />

When inflating the menu, you should call getSupportMenuInflater()since you are using ActionBarSherlock.

膨胀菜单时,您应该调用,getSupportMenuInflater()因为您正在使用ActionBarSherlock.

2- Programmatically:

2- 以编程方式:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem item = menu.add(Menu.NONE, ID, POSITION, TEXT);
    item.setIcon(R.drawable.drawable_resource_name);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return true;
}

Make sure you import com.actionbarsherlock.view.Menuand com.actionbarsherlock.view.MenuItem.

确保您导入com.actionbarsherlock.view.Menucom.actionbarsherlock.view.MenuItem.

回答by ebernie

What worked for me was using 'always|withText'. If you have many menus, consider using 'ifRoom' instead of 'always'.

对我有用的是使用' always|withText'。如果您有很多菜单,请考虑使用“ifRoom”而不是“always”。

<item android:id="@id/resource_name"
android:title="text"
android:icon="@drawable/drawable_resource_name"
android:showAsAction="always|withText" />

回答by Gilad Zelniker

The solution I find is to use custom action Layout: Here is XML for menu.

我找到的解决方案是使用自定义操作布局:这是菜单的 XML。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:Eventapp="http://schemas.android.com/apk/res-auto">
<!-- This is a comment. -->
    <item
        android:id="@+id/action_create"
        android:actionLayout="@layout/action_view_details_layout"
        android:orderInCategory="50"
        android:showAsAction = "always"/>
</menu>

The Layout is

布局是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="5dp"
    android:gravity="center"
    android:text="@string/create"/>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:gravity="center"
    android:src="@drawable/ic_action_v"/>

</LinearLayout>

this will show the icon and the text together.

这将同时显示图标和文本。

To get the clickitem the the fragment or activity:

要获取片段或活动的 clickitem:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    //super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_details_fragment, menu);
    View view = menu.findItem(R.id.action_create).getActionView();
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Clicked", Toast.LENGTH_SHORT).show();
        }
    });
}

回答by Alen Zarkovic

If any1 in 2017 is wondering how to do this programmatically, there is a way that i don't see in the answers

如果 2017 年的 any1 想知道如何以编程方式执行此操作,那么我在答案中看不到一种方法

.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

回答by Nail Shaykhraziev

You can add button in toolbar

您可以在工具栏中添加按钮

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="title">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginRight="16dp"
            android:background="@color/transparent"
            android:drawableRight="@drawable/ic_your_icon"
            android:drawableTint="@drawable/btn_selector"
            android:text="@string/sort_by_credit"
            android:textColor="@drawable/btn_selector"
            />

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

create file btn_selector.xmlin drawable

在 drawable 中创建文件btn_selector.xml

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

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

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

<item
    android:state_selected="true"
    android:color="@color/white"
    />
<item
    android:color="@color/white_30_opacity"
    />

java:

爪哇:

private boolean isSelect = false;

私人布尔 isSelect = false;

OnClickListener for button:

按钮的 OnClickListener:

private void myClick() {
    if (!isSelect) {
       //**your code**//
        isSelect = true;
    } else {//**your code**//
        isSelect = false;
    }
    sort.setSelected(isSelect);
}

回答by Freddie Jin

Try adding a TextView to the menubar first and using setCompoundDrawables()to place the image on whichever side you want. Bond click activity to the textview in the end.

尝试先将 TextView 添加到菜单栏,然后​​使用setCompoundDrawables()将图像放置在您想要的任何一侧。最后将点击活动绑定到文本视图。

MenuItem item = menu.add(Menu.NONE, R.id.menu_item_save, 10, R.string.save);
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS|MenuItem.SHOW_AS_ACTION_WITH_TEXT);
TextView textBtn = getTextButton(btn_title, btn_image);
item.setActionView(textBtn);
textBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
           // your selector here   }
    });

You can literally customize everything here:

您可以在此处自定义所有内容:

public TextView getTextButton (String btn_title, Drawable btn_image) {
    TextView textBtn = new TextView(this);
    textBtn.setText(btn_title);
    textBtn.setTextColor(Color.WHITE);
    textBtn.setTextSize(18);
    textBtn.setTypeface(Typeface.create("sans-serif-light", Typeface.BOLD));
    textBtn.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
    Drawable img = btn_image;
    img.setBounds(0, 0, 30, 30);
    textBtn.setCompoundDrawables(null, null, img, null); 
    // left,top,right,bottom. In this case icon is right to the text

    return textBtn;
}

回答by Think Twice Code Once

Some of you guys have great answers, but I found some additional thing. If you want create a MenuItem with some SubMenu programmatically:

你们中的一些人有很好的答案,但我发现了一些额外的东西。如果你想以编程方式创建一个带有一些子菜单的 MenuItem:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        SubMenu subMenu = menu.addSubMenu(0, Menu.NONE, 0, "Menu title");
        subMenu.getItem().setIcon(R.drawable.ic_action_child);
        subMenu.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        subMenu.add(0, Menu.NONE, 0, "Subitem 1");
        subMenu.add(0, Menu.NONE, 1, "Subitem 2");
        subMenu.add(0, Menu.NONE, 2, "Subitem 3");

        return true;
    }

回答by desu sudarsana

Using app:showAsAction="always|withText". I am using Android 4.1.1, but it should applicable anyway. Mine look like below

使用 app:showAsAction="always|withText"。我使用的是 Android 4.1.1,但它应该适用。我的看起来像下面

<item
        android:id="@+id/action_sent_current_data"
        android:icon="@drawable/ic_upload_cloud"
        android:orderInCategory="100"
        android:title="@string/action_sent_current_data"
        app:showAsAction="always|withText"/>

回答by rao

Follow these steps:

按着这些次序:

  1. Add the Action Bar instance in the Java Code final ActionBar actionBar = getActionBar();
  2. Enable the Home Display Option actionBar.setDisplayShowHomeEnabled(false);
  3. Add the following code in the respective activity's manifest file android:logo=@drawable/logo and android:label="@string/actionbar_text"
  1. 在 Java 代码中添加 Action Bar 实例 final ActionBar actionBar = getActionBar();
  2. 启用主页显示选项 actionBar.setDisplayShowHomeEnabled(false);
  3. 在相应活动的清单文件中添加以下代码 android:logo=@drawable/logo and android:label="@string/actionbar_text"

I think this will help you

我想这会帮助你