如何在按下时设置MenuItems工具栏的样式

时间:2014-11-18 08:59:54

标签: android onclick styles toolbar optionmenu

在KitKat中,我在受支持的库工具栏中有一个样式化的选项菜单,定义如下

    <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:gravity="center"
    android:background="?attr/colorPrimary"
    app:theme="@style/ToolbarText"
    app:popupTheme="@style/optionMenu" 
    />

其中optionMenu样式为

<style name="optionMenu">
    <item name="android:textColor">@color/menu_text</item>  
    <item name="android:background">@color/menu_background</item>

</style>

它按预期工作,但是当我按下MenuItems时,它不会在所有可用空间中采用“默认灰色”颜色,而是仅在菜单元素周围的边缘上,其背景保持颜色,如样式中所定义。我的目标不是改变压缩事件的风格,我只希望文本背后的背景在按下时看起来像整个矩形按钮

我哪里错了?

2 个答案:

答案 0 :(得分:2)

最后在默认库中查看了一天以上后,我通过以下调整在我的应用程序中工作:

布局文件

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/ToolBarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/toolbar_color"
    android:minHeight="@dimen/abc_action_bar_default_height_material" />

<强>样式

 <style name="ToolBarStyle" parent="">
    <item name="android:elevation">@dimen/toolbar_elevation</item>
    <item name="popupTheme">@style/ItemMenuBackground</item>
    <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>

<style name="ItemMenuBackground" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:colorBackground">@android:color/white</item>
    <item name="listChoiceBackgroundIndicator">@android:color/white</item>
</style>

在我刚刚提到的@dimen/toolbar_elevation&#34; 4dp&#34;但这不是问题。使用<item name="android:colorBackground">我可以设置正常背景,使用<item name="listChoiceBackgroundIndicator">我可以设置菜单项的按下背景。

我希望这有帮助

答案 1 :(得分:0)

添加您自己的菜单按钮并相应地设置它们,下面是如何执行此操作的示例:

<android.support.v7.widget.Toolbar
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/toolbar"
   android:minHeight="?attr/actionBarSize"
   android:layout_height="?attr/actionBarSize"
   android:layout_width="match_parent">

   <!-- this adds a button at the center of the toolbar -->
   <ImageButton
       android:id="@+id/button"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:onClick="buttonHandler"
       android:src="@drawable/ic_launcher"
       android:background="@drawable/backgroundStates"
       android:layout_gravity="center"/>

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

现在,在您的drawable资源中,添加名为backgroundStates.xml的文件,其中包含以下内容:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true" android:drawable="@android:color/holo_blue_light"/>
     <item  android:drawable="@android:color/darker_gray" />
 </selector>

只要有人点击工具栏中的已定义按钮,上面的内容就会将颜色从灰色变为holo_blue_light。

现在,在您的活动中创建一个函数来处理定义按钮上的onClicks:

 public void buttonHandler(View v){

  // Do stuf you want that should happen on button pressed, you may want to inflate a list view or other stuff ...
  }