【问题标题】:Make a toolbar with menu icon like amazon app制作带有菜单图标的工具栏,如亚马逊应用程序
【发布时间】:2019-01-13 17:58:50
【问题描述】:

我正在尝试制作一个带有菜单图标的工具栏,例如亚马逊应用程序。但是工具栏菜单图标没有正确设置。实际上所有菜单图标已设置为从左到右。我需要返回图标,导航图标设置在右侧,应用程序徽标设置在中心,其他图标设置在左侧。

菜单代码...

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item
        android:id="@+id/action_bookmark"
        android:icon="@drawable/ic_bookmark_white_24dp"
        android:orderInCategory="100"
        android:title="@string/action_bookmark"
        app:showAsAction="always" />

    <item
        android:id="@+id/action_back"
        android:icon="@drawable/ic_keyboard_arrow_left_white_24dp"
        android:orderInCategory="101"
        android:title="@string/action_back"
        app:showAsAction="always" />

    <item
        android:id="@+id/texttite"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="102"
        android:title="@string/action_forward"
        android:titleCondensed="hello........"
        app:showAsAction="always" />


    <item
        android:id="@+id/action_forward"
        android:icon="@drawable/ic_keyboard_arrow_right_white_24dp"
        android:orderInCategory="103"
        android:title="@string/action_forward"
        app:showAsAction="always" />

    <item
        android:id="@+id/action_bookmark1"
        android:icon="@drawable/ic_bookmark_white_24dp"
        android:orderInCategory="104"

        android:title="@string/action_bookmark"
        app:showAsAction="always" />


</menu>

我需要这种类型的工具栏。我试试这个,但没有成功。请帮帮我...

【问题讨论】:

  • 您必须将视图添加到工具栏(设计工具栏)。您不能使用菜单膨胀来获得类似亚马逊应用程序的工具栏。
  • 怎么用,请举例..
  • 为什么不用自定义布局在工具栏中或使用自定义布局代替工具栏
  • 先生,请举个例子..

标签: android android-layout android-studio android-toolbar android-design-library


【解决方案1】:

您可以根据需要自定义工具栏布局,就像任何其他布局一样。请在下面找到仅使用按钮的示例布局。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorBlack">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">

                <Button
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:text="<" />

                <Button
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:text="=" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Custom"
                    android:textColor="@color/backgroundColorGenericWhite"
                    android:textSize="22sp" />
            </LinearLayout>

            <Button
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="=" />
        </LinearLayout>

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

【讨论】:

  • 感谢先生的回答。但我使用的是菜单项,而不是按钮。
【解决方案2】:

这是自定义工具栏代码

  <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:padding="0px"
                android:layout_margin="0px"
                app:popupTheme="@style/AppTheme.PopupOverlay" >
            </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

【讨论】:

    猜你喜欢
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 2017-11-30
    • 2018-07-09
    相关资源
    最近更新 更多