添加菜单项文字& titlecase工具栏中的文字android

时间:2016-02-16 12:35:23

标签: android xml android-layout xamarin

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

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

enter image description here

5 个答案:

答案 0 :(得分:2)

使用下面的菜单文件 -

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

答案 1 :(得分:1)

如果您使用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>

然后,结果如下:

enter image description here

答案 2 :(得分:1)

您需要为声明的菜单项设置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 :(得分:0)

要更改文字颜色,试试这个,我还没有测试,但它应该有效:

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

答案 4 :(得分:0)

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

  <bool name="abc_config_actionMenuItemAllCaps">false</bool>
相关问题