2017-06-20 67 views
2

我有一個問題,在操作欄中增加MenuItem的寬度,它採用固定寬度。下面是屏幕截圖,如何使用Actionbar在android中增加菜單項寬度

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

請給你的建議,如何解決這個問題。

感謝

+0

試試這個[第一個](https://stackoverflow.com/questions/38373481/remove-large-padding-after-hamburger-menu-icon-in-android-toolbar)和[second](https:// stackoverflow.com/questions/9737101/padding-between-actionbars-home-icon-and-title)解決方案! –

回答

2

與哈克做到了。

我知道這不是最好的或者甚至是很好的解決方案,但我是用黑客做的。

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

可以特定寬度傳遞給視圖,這樣它會採取在-至少那麼多空間。