为操作栏菜单项使用自定义模板
Using a custom template for actionbar menu item
我正在尝试显示文本和图标始终可见的菜单。我已将我的项目布局定义如下
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:drawableLeft="@drawable/ic_cart"
android:gravity="center"
android:orientation="vertical"
android:text="(0)"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="@android:color/white" />
菜单定义如下
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.taptoscan.taptoscan.MainActivity">
<item
android:id="@+id/action_sign"
android:title="(0)"
app:showAsAction="always|withText"
app:actionLayout="@layout/menu_sign" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
我在启用背景涟漪效果时遇到了问题。它有效,但涟漪效果不是圆形的,就像在常规菜单图标上一样。它是方形的,看起来有点丑。在自定义菜单项上实现本机波纹效果外观的最佳方法是什么?
编辑:这就是它的样子
注意:找到更好的解决方案,检查编辑
我明白了。首先我创建了一个带有圆角的背景可绘制对象,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<corners android:radius="5dp" />
</shape>
</ripple>
之后,我将 TextView 的背景设置为上面的可绘制对象。波纹效果按预期工作,并且像默认菜单项一样是圆形的。
编辑
在进一步研究自定义菜单项之后,我发现仅使用不带根元素的 TextView 会导致 TextView 的基线在单击另一个菜单项然后再单击自定义菜单项后发生变化.解决方案是使用以下布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="@drawable/btn_actionbar_icon">
<TextView
android:id="@+id/icon_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_cart"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:padding="10dp"
android:text="(0)"
android:textColor="@android:color/white"
android:textSize="14sp" />
</RelativeLayout>
我正在尝试显示文本和图标始终可见的菜单。我已将我的项目布局定义如下
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:drawableLeft="@drawable/ic_cart"
android:gravity="center"
android:orientation="vertical"
android:text="(0)"
android:drawablePadding="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="@android:color/white" />
菜单定义如下
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.taptoscan.taptoscan.MainActivity">
<item
android:id="@+id/action_sign"
android:title="(0)"
app:showAsAction="always|withText"
app:actionLayout="@layout/menu_sign" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>
我在启用背景涟漪效果时遇到了问题。它有效,但涟漪效果不是圆形的,就像在常规菜单图标上一样。它是方形的,看起来有点丑。在自定义菜单项上实现本机波纹效果外观的最佳方法是什么?
编辑:这就是它的样子
注意:找到更好的解决方案,检查编辑
我明白了。首先我创建了一个带有圆角的背景可绘制对象,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<corners android:radius="5dp" />
</shape>
</ripple>
之后,我将 TextView 的背景设置为上面的可绘制对象。波纹效果按预期工作,并且像默认菜单项一样是圆形的。
编辑
在进一步研究自定义菜单项之后,我发现仅使用不带根元素的 TextView 会导致 TextView 的基线在单击另一个菜单项然后再单击自定义菜单项后发生变化.解决方案是使用以下布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="@drawable/btn_actionbar_icon">
<TextView
android:id="@+id/icon_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_cart"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:padding="10dp"
android:text="(0)"
android:textColor="@android:color/white"
android:textSize="14sp" />
</RelativeLayout>