Android 为actionbar菜单项使用自定义模板
我试图显示一个始终可见文本和图标的菜单。我已将我的项目布局定义如下Android 为actionbar菜单项使用自定义模板,android,menu,android-actionbar,Android,Menu,Android Actionbar,我试图显示一个始终可见文本和图标的菜单。我已将我的项目布局定义如下 <?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:backgr
<?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"?>
<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的背景设置为可从上面绘制的背景。Ripple effect按预期工作,它像默认菜单项一样全面
编辑
在进一步搞乱了自定义菜单项之后,我发现仅仅使用一个没有根元素的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>
唯一的方法是创建一个扩展TextView的自定义视图,然后应用一些逻辑来创建它round@MohamedRa设法想出了一个解决方案,并将其作为答案发布。