ActionBar with icon and text in portrait mode

前端 未结 3 1740
梦毁少年i
梦毁少年i 2021-01-18 04:35

I can\'t seem to get an image and text to work in portrait mode on menu items in the action bar no matter what I try. I even tried to force android portrait orientation

相关标签:
3条回答
  • From the config.xml file of the native ActionBar:

    values:

    <bool name="config_allowActionMenuItemTextWithIcon">false</bool>
    

    values-480dp:

    <bool name="config_allowActionMenuItemTextWithIcon">true</bool>
    

    To answer your question:

    No you can't do that, if the devices width is smaller than 480dp. (Unless you want to create a custom rom with a modded framework)

    0 讨论(0)
  • 2021-01-18 04:55

    The easiest way to do it is:

    <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:ignore="AppCompatResource">
    <item
        android:id="@+id/action_nexo"
        android:title="title"
        app:actionLayout="@layout/custom_menu_item"
        app:showAsAction="always"/>
    
     </menu>
    

    custom_menu_item.xml

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="?attr/actionBarSize">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/nexo_tile_short"
        android:clickable="true"
        android:focusable="true"
        android:textSize="@dimen/text_size_small"
        android:textStyle="bold"
        android:textColor="?attr/actionMenuTextColor"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:paddingTop="@dimen/padding_small"
        android:paddingBottom="@dimen/padding_small"
        android:src="@drawable/nexo"/>
     </LinearLayout>
    
    0 讨论(0)
  • 2021-01-18 05:01

    Ahmad's answer makes clear why this is not possible. It is annoying that the action bar is not easier to customise.

    One quick and dirty solution is to put two buttons in the menu, remove the icon from one and give the second a different name. Then in the corresponding java file, replicate the functionality for the extra button in onOptionsItemSelected. This gets over having to create a custom view for the action bar.

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
        <item android:id="@+id/action_done"
              android:icon="@drawable/ic_done"
              android:title="@string/done"
              yourapp:showAsAction="always" />
        <item android:id="@+id/action_done2"
              android:title="@string/done"
              yourapp:showAsAction="always" />
        <item android:id="@+id/action_cancel"
              android:icon="@drawable/ic_cancel"
              android:title="@string/cancel"
              yourapp:showAsAction="always" />
        <item android:id="@+id/action_cancel2"
              android:title="@string/cancel"
              yourapp:showAsAction="always" />
    </menu>
    

    I also saw this answer but didn't try it: withText in split ActionBar #Google Calendar Method. This is how it is done in Google Calendar but also relies on a custom view replacing the action bar.

    0 讨论(0)
提交回复
热议问题