【问题标题】:Add menu item text & text in titlecase toolbar android在标题栏工具栏android中添加菜单项文本和文本
【发布时间】:2016-02-16 12:35:23
【问题描述】:

我使用此代码,我想添加 action1 和 action2,例如此文本的图片和颜色变化

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    />

菜单

  <?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/ok" android:text="action1" app:showAsAction="never" />
        <item android:id="@+id/cancel" android:text="action2"  app:showAsAction="ifRoom" />
    </menu>

我的风格

 <style name="MyTheme" parent="MyTheme.Base">   
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">false</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#2196F3</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#1976D2</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#FF4081</item>   
     <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. --> 
  </style> 

我的工具栏

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/theme_color"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

【问题讨论】:

  • 不向 menuItems 提供图标
  • 使用 app:showAsAction="always"
  • @kevz 菜单出现在工具栏中,但没有出现文本
  • 即使你设置了 app:showAsAction="always"?
  • 我使用的总是项目出现在屏幕上,但文字不可见

标签: android xml android-layout xamarin


【解决方案1】:

使用下面的菜单文件-

<?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/ok" app:showAsAction="always"
        android:title="Action1" />
    <item android:id="@+id/cancel" app:showAsAction="always"
        android:title="Action2" />
</menu>

你需要设置android:title="action2"

【讨论】:

  • 我认为我的菜单项文本颜色存在问题,所以您能提供更改菜单项颜色的链接
  • 我写了 androd:title="Action",它看起来像“ACTION”,我希望它看起来像“Action”
  • @Kishan: 请张贴你的styles.xml
  • @Kishan: wts 最小 sdk 吗?
  • @kvz 最低安卓版本为 Override-Android 2.3 (Api level 10)
【解决方案2】:

您需要为声明的菜单项设置 android:title 属性而不是 android:text。

<?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/ok" android:title="action1" app:showAsAction="" />
    <item android:id="@+id/cancel" android:title="action2"  app:showAsAction="ifRoom" />
</menu>

【讨论】:

    【解决方案3】:

    如果您使用AppCompat_v7 操作栏反向移植,则需要同时使用这两个:

    android:showAsActionapp:showAsActionalways

    查看此链接:Items not showing in the ActionBar with showAsAction=“always”

    您需要使用setTitle 而不是这样的名称:

    <?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/ok"
            app:showAsAction="always"
            android:title="Action1" />
        <item
            android:id="@+id/cancel"
            app:showAsAction="always"
            android:title="Action2" />
    </menu>
    

    那么,结果如下:

    【讨论】:

    • 这是另一个问题,请使用提问。
    【解决方案4】:

    要更改文本颜色,试试这个,我没有测试过,但应该可以:

    <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:background="#000"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/header"
            app:itemTextColor="your color"
            app:menu="@menu/your menu file" />
    

    【讨论】:

      【解决方案5】:

      对于标题大小写中的菜单项文本,您可以在 xml 文件中添加代码

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

      【讨论】:

        猜你喜欢
        • 2017-09-02
        • 2015-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多