我正在做一个简单的项目,我想尽可能避免使用 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 将设置 minWidth 和 minHeight:
<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_width 和 ShapeDrawable_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/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问