我可以让工具栏有两行仍然使用菜单项吗?

时间:2015-04-06 01:49:28

标签: android android-actionbar android-appcompat android-toolbar

我使用新的appcompat工具栏作为操作栏。我想使用android:showAsAction="ifRoom"的菜单项,但我还希望工具栏下方有按钮,这些按钮是工具栏布局的一部分。不是菜单项,而是我自己添加的按钮和文本视图。希望有道理。无论如何我该怎么做呢?理想情况下,整个第二行只有我的项目,而顶行则有菜单项。

感谢。

编辑:这就是我所说的:

<android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="128dp"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:hint="should extend full width" />
        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

看起来像这样:

enter image description here

2 个答案:

答案 0 :(得分:0)

这实际上不是你想要的那样自定义工具栏,因为菜单是由你无法处理的系统控制的。

但你可以做一个解决方法来实现这样的效果,

<android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            </android.support.v7.widget.Toolbar>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:orientation="horizontal"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:background="?attr/colorPrimary">

                <EditText
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/white"
                    android:hint="Hello" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:text="Button"/>
            </LinearLayout>
       <!--Other layout-->

或者将工具栏和线性布局包装在另一个线性布局中以将它们绑定到聚集。

它应该解决你的目的!

答案 1 :(得分:-2)

您始终可以使用反射来执行此操作:

Toolbar toolbar = findViewById(R.id.my_awesome_toolbar);
toolbar.setTitle("A really long title here to make sure we wrap text.");
    Field mTitleTextViewField = null;
    try {
        mTitleTextViewField = Toolbar.class.getDeclaredField("mTitleTextView");
        mTitleTextViewField.setAccessible(true);
        TextView mTitleTextView = (TextView)mTitleTextViewField.get(toolbar);
        mTitleTextView.setSingleLine(false);
        mTitleTextView.setMaxLines(2);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }