toolbar中设置menu收起图标颜色以及popupwindow弹出位置

1、设置menu收起图标颜色,就是那三个点的图标这里写图片描述,默认的如果使用light style 就是 黑色的,如果使用 dark style 就是白色的。
如何能够自定义颜色?

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!-- 添加这一句, 即可改变图标的颜色-->
        <item name="android:textColorSecondary">#ffffff</item>
    </style>

2、一般情况下,点击收起menu的按钮,popwindow会覆盖在toolbar上,不美观,如何能够让popwindow在toolbar下方显示呢?

 <style name="ToolbarPopupTheme" parent="@style/ThemeOverlay.AppCompat.Dark">
        <!--<item name="android:colorBackground">#000000</item> 也可以设置背景色以及menu中的其他属性-->
        <item name="actionOverflowMenuStyle">@style/OverflowMenuStyle</item> 
    </style>

    <style name="OverflowMenuStyle" parent="Widget.AppCompat.Light.PopupMenu.Overflow">
        <item name="overlapAnchor">false</item>  <!--设置不覆盖锚点-->
    </style>

然后在toolbar中设置app:popupTheme

 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:actionBarSize"
        android:background="?attr/colorPrimary"
        app:navigationIcon="@mipmap/back"
        app:popupTheme="@style/ToolbarPopupTheme">
</android.support.v7.widget.Toolbar>

效果如下
这里写图片描述

你可以通过在Toolbarmenu布局文件设置每个MenuItem的actionLayout属性为一个自定义的单选按钮布局文件,然后在该布局文件设置单选按钮的颜色。下面是一个示例代码: 首先,在menu布局文件设置actionLayout属性: ``` <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/my_menu_item" android:title="My Menu Item" app:showAsAction="always" app:actionLayout="@layout/menu_item_radio_group"/> </menu> ``` 然后,创建一个名为menu_item_radio_group.xml的布局文件,其包含一个RadioGroup和两个RadioButton: ``` <?xml version="1.0" encoding="utf-8"?> <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radio_button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" android:checked="true" android:textColor="@color/white"/> <RadioButton android:id="@+id/radio_button_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" android:textColor="@color/white"/> </RadioGroup> ``` 最后,在Activity的onCreateOptionsMenu方法获取MenuItem并设置它的点击事件: ``` @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my_menu, menu); MenuItem menuItem = menu.findItem(R.id.my_menu_item); RadioGroup radioGroup = (RadioGroup) menuItem.getActionView(); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // Handle radio button click events here } }); return true; } ``` 注意,这里通过menuItem.getActionView()方法获取了RadioGroup布局,而不是直接调用findViewById方法。这是因为actionLayout属性指定的布局文件不会自动加载到MenuItem,需要手动获取并添加到MenuItem的ActionView。 另外,还需要在colors.xml文件定义单选按钮的颜色: ``` <resources> <color name="white">#FFFFFF</color> </resources> ``` 这样就可以通过设置单选按钮的textColor属性来改变它的颜色了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值