【问题标题】:How to styling MenuItems Toolbar when pressed按下时如何设置 MenuItems 工具栏的样式
【发布时间】:2015-06-24 14:27:31
【问题描述】:

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

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

我哪里错了?

【问题讨论】:

    标签: android onclick styles toolbar optionmenu


    【解决方案1】:

    最后在默认库中浏览了一天多之后,我通过以下调整让它在我的应用程序下工作:

    布局文件

    <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 下,我刚刚提到了“4dp”,但这不是问题所在。使用&lt;item name="android:colorBackground"&gt;,我可以设置普通背景,使用&lt;item name="listChoiceBackgroundIndicator"&gt;,我可以设置菜单项的按下背景。

    希望对你有帮助

    【讨论】:

    • 您知道如何为正常的操作菜单项执行此操作吗?目前,当使用样式“ThemeOverlay.AppCompat.Dark.ActionBar”时,它将背景选择器设置为 Lollipop 及以上的白色波纹。
    • @androiddeveloper ,我不确定,因为提出的问题不是针对材料设计的。但我认为可绘制资源可能会做到这一点。基本上,您需要将颜色资源替换为可绘制资源以实现涟漪效果。还要确保包括用于棒棒糖前设备的可绘制对象。链接stackoverflow.com/a/26686433/1161911 可能会有所帮助。
    • 不错。可能有用。你试过了吗?但是如何将其设置为工具栏?
    • @androiddeveloper 是的,我已经在我的项目中尝试过它,它就像魅力一样。我已经使用 style 属性在 Toolbar 元素中添加了“ToolBarStyle”。
    【解决方案2】:

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

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

    现在,在您的可绘制资源中,添加名为 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。

    现在,在您的 Activity 中创建一个函数来处理已定义按钮上的 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 ...
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多