【问题标题】:Custom layout in toolbar not showing工具栏中的自定义布局未显示
【发布时间】:2015-03-06 02:46:00
【问题描述】:

我已使用 v7 支持库将 supportActionbar 添加到我的应用程序中。现在我需要在操作栏上显示一个自定义布局,它只是一个带有数字徽章的购物车图标。但是,当我尝试放置它时,我只会得到一个文本按钮,并且没有显示任何图像。我的布局是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/cart_img"
android:clickable="true"
android:src="@drawable/ic_checkout"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:textColor="@color/colorAccent"
android:text="0"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

然后我使用

将它添加到操作栏
<item
android:id="@+id/ab_cart"
android:icon="@drawable/ic_checkout"
android:actionLayout="@layout/cart_badge"
android:title="@string/action_cart"
app:showAsAction="always" />

但我只在操作栏中获得文本按钮“购物车”。

【问题讨论】:

  • 你的工具栏在哪里?你是动态创建的吗?
  • 我在一个布局上创建了它,并将它包含在我的 Activity 布局中。无论如何,我现在已经解决了。好像我不得不把 app:actionLayout 而不是 android.

标签: android android-layout android-actionbar android-toolbar


【解决方案1】:

解决了。这是非常简单的修复。我必须将android:actionLayout="@layout/cart_badge" 替换为app:actionLayout="@layout/cart_badge"

【讨论】:

  • 您在使用工具栏吗?我有同样的问题,但这个解决方案不起作用
猜你喜欢
  • 2015-10-18
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
相关资源
最近更新 更多