gpt4 book ai didi

android - 当 ShapeDrawable 设置为图标时,ActionBar 项目不显示

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:25:10 24 4
gpt4 key购买 nike

我正在做一个简单的项目,我想尽可能避免使用 PNG 图像。我需要一个“+”(添加)按钮,我使用带有给定代码的 ShapeDrawable 创建了它。

res/drawable/plus_icon_drawable.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="line">
<stroke android:color="#000"
android:width="3dp"/>
</shape>
</item>
<item>
<rotate
android:fromDegrees="90"
android:toDegrees="90">
<shape android:shape="line">
<stroke android:color="#000"
android:width="3dp"/>
</shape>
</rotate>
</item>
</layer-list>

当我将它添加为 Button 或 ImageButton 的背景时,这段代码工作正常,但是当我将它添加为 ActionBar 项目的图标时,该项目没有显示。

当我将 PNG 图像设置为图标时,效果很好。 ShapeDrawables 是否有任何限制阻止它在 ActionBar 上呈现?

这是我的 ActiobBar 代码

res/menu/action_my_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_some_id"
android:icon="@drawable/plus_icon_drawable"
android:title="My Action"
android:showAsAction="always"/>
</menu>

更新(2014 年 7 月 8 日):已使用 Native 和 AppCompat 进行测试。实际上该项目存在于那里,但 ShapeDrawable 图像未被渲染。操作项响应点击。

我在这里错过了什么?

最佳答案

Is there any restriction for the ShapeDrawables which blocks it from getting rendered on ActionBar?

不是那样的。事情就是这样。这就是您看到此信息的原因:

Layer/ShapeDrawables 没有默认大小。换句话说,ShapeDrawable 的宽度和高度为零是完全没问题的。 stroke-width 参数也没有规定任何内容。

This code works fine when I add it as a background of Button or an ImageButton but when I add this as an icon of an ActionBar item, the item is not getting displayed.

当然可以。 ImageButtons/Buttons 有填充/最小宽度/最小高度的概念。这些允许 ShapeDrawable 有一些呼吸的空间。例如,使用 Widget.Holo.Button 设置样式的 Button 将设置 minWidthminHeight:

<item name="android:minHeight">48dip</item>
<item name="android:minWidth">64dip</item>

这里的有效测试是:

定义一个ImageView如下:

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

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add" />

</RelativeLayout>

ImageView 在上面的定义中没有大小指南——我们使用的是 wrap_content。什么都不会显示。另一方面,如果您使用 png 可绘制对象,它本身会定义大小。在上面的示例中,使用明确的大小(比如 50dp)将使 ShapeDrawable visible

这就是 ActionMenuItemView 设置可绘制对象的方式:

public void setIcon(Drawable icon) {
mIcon = icon;
if (icon != null) {

// Zero in case you don't define the <size />
int width = icon.getIntrinsicWidth();
int height = icon.getIntrinsicHeight();
if (width > mMaxIconSize) {
final float scale = (float) mMaxIconSize / width;
width = mMaxIconSize;
height *= scale;
}
if (height > mMaxIconSize) {
final float scale = (float) mMaxIconSize / height;
height = mMaxIconSize;
width *= scale;
}
icon.setBounds(0, 0, width, height);
}
setCompoundDrawables(icon, null, null, null);

updateTextButtonVisibility();
}

ShapeDrawable#inflate(...) 方法的 fragment :

 setIntrinsicWidth((int)
a.getDimension(com.android.internal.R.styleable.ShapeDrawable_width, 0f));
setIntrinsicHeight((int)
a.getDimension(com.android.internal.R.styleable.ShapeDrawable_height, 0f));

如果未定义 ShapeDrawable_widthShapeDrawable_height,固有宽度和高度将设置为零。这意味着,声明 icon.setBounds(0, 0, width, height); (来自 setIcon(Drawable) 方法)实际上是 => icon.setBounds(0, 0, 0 , 0);.

看来我们确实在处理没有尺寸信息的 ShapeDrawables

可能的修复:

<shape android:shape="line">
<size android:width="?android:attr/actionBarSize"
android:height="?android:attr/actionBarSize"/>
<stroke android:color="#000"
android:width="3dp"/>
</shape>

但这行不通 - 由于某些原因 ?android:attr/actionBarSize 不被视为维度。这很奇怪,因为 ?android:attr/actionBarSize 在布局情况下提供给 android:layout_width 时工作正常。

解决方法是定义一个新维度,比如 actionBarSizeDp 并设置它:

res/values/dimens.xml 中:

<dimen name="actionBarSizeDp">48dp</dimen>

res/values-land/dimens.xml 中:

<dimen name="actionBarSizeDp">40dp</dimen>

这些值是在 android 自己的 res/values-XX/dimens.xml 中定义的默认操作栏大小值。

最后,在plus_icon_drawable.xml中使用actionBarSizeDp:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="line">
<size android:width="@dimen/actionBarSizeDp"
android:height="@dimen/actionBarSizeDp"/>
<stroke android:color="#000"
android:width="3dp"/>
</shape>
</item>
<item>
<rotate
android:fromDegrees="90"
android:toDegrees="90">
<size android:width="@dimen/actionBarSizeDp"
android:height="@dimen/actionBarSizeDp"/>
<shape android:shape="line">
<stroke android:color="#000"
android:width="3dp"/>
</shape>
</rotate>
</item>
</layer-list>

事实上,在 LayerDrawables 之一上定义 size 就足够了。其他人将相应地绘制。

关于android - 当 ShapeDrawable 设置为图标时,ActionBar 项目不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24452384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com