【问题标题】:How to display both icon and title of action inside ActionBar?如何在 ActionBar 中同时显示图标和动作标题?
【发布时间】:2012-08-13 14:14:34
【问题描述】:

我需要在ActionBar 中同时显示icontitle 的动作。

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

【问题讨论】:

  • 这个问题最后解决了吗?如果有,需要做什么?
  • 看起来答案在这里:stackoverflow.com/questions/9282122/… 简而言之,这是设计使然,您必须提供自定义布局来解决它。

标签: android android-layout android-actionbar actionbarsherlock android-ui


【解决方案1】:
如果有足够的空间,

'always|withText' 将起作用,否则只会放置图标。您可以在手机上进行旋转测试。

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

【讨论】:

  • 嗨,Eng.Faouad,它对我不起作用,我们必须以编程方式进行任何更改吗?
  • 即使有很多房间也不行。图标和文字只能横向显示。
  • @YiboLong 这个答案已被弃用,因为程序通常使用 appCompact 库
  • @murt 这个答案和 cmets 对于 appcompat 库仍然有效。
【解决方案2】:

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

1- 来自 XML:

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

膨胀菜单时,您应该调用getSupportMenuInflater(),因为您使用的是ActionBarSherlock

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;
}

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

【讨论】:

  • MenuItem item = menu.add(Menu.NONE, ID, POSITION, TEXT); 这里的 ID 是布局 id(例如 R.layout.myMenuItem),POSITION 是显示在操作栏中的项目的位置(例如 1,2,3 rtl),TEXT 是您要设置为操作栏项目的标题。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    我找到的解决方案是使用自定义动作布局: 这是菜单的 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>
    

    布局是

    <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>
    

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

    获取片段或活动的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();
            }
        });
    }
    

    【讨论】:

    • 如果有人收到此错误“错误:(4)在包'android'中找不到属性'actionlayout'的资源标识符”,可以通过将“android:actionLayout”替换为“app:actionLayout”来解决"
    • 涟漪效应消失了
    • 不知何故这对我没有显示 - 如果我提供它,它只是忽略布局并且不显示任何内容或标题(顺便说一下,省略标题会发出警告,但它会编译)
    • @CharlesGalvez 您可以将android:background="?selectableItemBackground"LinearLayout 以及android:clickable="true"android:focusable="true" 一起放置
    • 可能这行得通,我不确定,因为在我的情况下,菜单中没有显示任何内容,但按下了一个按钮。我认为它在白色工具栏上绘制了白色文本。所以要覆盖 textcolor 见stackoverflow.com/questions/43276345/….
    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

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

      <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>
      

      在drawable中创建文件btn_selector.xml

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

      &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;

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

      java:

      私有布尔 isSelect = false;

      按钮的 OnClickListener:

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

      【讨论】:

      • 将按钮添加到工具栏确实有效,尽管 Android Studio 说“此处不允许元素按钮 ...”。 (只是尝试了那部分。没有实现完整的解决方案。所以我不知道,是否所有的东西都有效。)
      【解决方案7】:

      你们中的一些人有很好的答案,但我发现了一些额外的东西。如果您想以编程方式创建带有某些 SubMenu 的 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;
          }
      

      【讨论】:

        【解决方案8】:

        尝试先将 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   }
            });
        

        您可以在这里自定义所有内容:

        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;
        }
        

        【讨论】:

          【解决方案9】:

          使用应用程序: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"/>
          

          【讨论】:

            【解决方案10】:

            按照以下步骤操作:

            1. 在Java代码final ActionBar actionBar = getActionBar();中添加Action Bar实例
            2. 启用主页显示选项 actionBar.setDisplayShowHomeEnabled(false);
            3. 在相应活动的清单文件中添加以下代码 android:logo=@drawable/logo and android:label="@string/actionbar_text"

            我想这会对你有所帮助

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-03-23
              • 1970-01-01
              相关资源
              最近更新 更多