gpt4 book ai didi

android - 如何在使用 Actionbar 时增加 android 中的菜单项宽度

转载 作者:搜寻专家 更新时间:2023-11-01 08:26:16 25 4
gpt4 key购买 nike

我在操作栏中增加 MenuItem 的宽度时遇到问题,它采用固定宽度。下面是屏幕截图,

enter image description here

并希望实现以下目标,

enter image description here

代码如下:

search_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@drawable/search_view_background"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content">
<TextView android:layout_width="0dp"
android:padding="10dp"
android:layout_weight="1"
android:text="Search Product"
android:textColor="@color/grey"
android:gravity="center_vertical"
android:drawableEnd="@drawable/ic_search"
android:drawableRight="@drawable/ic_search"
android:drawablePadding="20dp"
android:textSize="@dimen/text_size_14sp"
android:layout_height="match_parent"/>
</LinearLayout>

home_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/search"
android:onClick="onClickSearchButton"
android:orderInCategory="1"
android:title="@string/Search"
app:actionLayout="@layout/search_layout"
app:showAsAction="always"/>
<item
android:id="@+id/action_cart"
android:icon="@drawable/ic_menu_cart"
android:orderInCategory="2"
android:title="@string/action_cart"
app:showAsAction="always"/>

<item
android:id="@+id/overflow"
android:icon="@drawable/ic_menu_notifications"
android:orderInCategory="3"
android:title="@string/overFlowMenu"
android:visible="false"
app:showAsAction="always"/>

请给出你的建议,如何解决这个问题。

谢谢

最佳答案

通过 Hack 实现。

我知道这不是最好的解决方案,甚至不是很好的解决方案,但我用 hack 做到了。

searchItem.getActionView().findViewById(R.id.text).setMinimumWidth(width);

您可以将特定宽度传递给 View ,这样它至少会占用那么多空间。

关于android - 如何在使用 Actionbar 时增加 android 中的菜单项宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44654693/

25 4 0