How to increase the menuitem width in android while using Actionbar

核能气质少年 提交于 2021-02-07 21:37:18

问题


I have a problem to increase the width of MenuItem in Action bar, it take the fixed width. Below is screen shot,

and want to achieve the below one,

Below is the code:

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

Please give your suggestion, how to fix this problem.

Thanks


回答1:


Did it with a Hack.

I know it's not the best or even good solution but I did it with an hack.

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

You can pass a particular width to the view so that it will take at-least that much space.



来源:https://stackoverflow.com/questions/44654693/how-to-increase-the-menuitem-width-in-android-while-using-actionbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!