如何在使用Actionbar时增加Android中的menuitem宽度 [英] How to increase the menuitem width in android while using Actionbar

查看:66
本文介绍了如何在使用Actionbar时增加Android中的menuitem宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

并希望实现以下目标,

下面是代码:

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.

谢谢

推荐答案

使用Hack对其进行了

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.

这篇关于如何在使用Actionbar时增加Android中的menuitem宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆