草庐IT

android - Shape Drawable 参数取决于应用的样式

coder 2023-11-25 原文

假设我有一个简单的可绘制形状,它绘制了一个环,如下所示:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:innerRadiusRatio="3"
    android:shape="ring"
    android:thicknessRatio="36"
    android:useLevel="false" >

    <gradient
        android:centerColor="@android:color/holo_blue_bright"
        android:centerY="0.001"
        android:endColor="@android:color/holo_blue_dark"
        android:gradientRadius="202.5"
        android:startColor="@android:color/transparent"
        android:type="radial"
        android:useLevel="false" />

</shape>

然后我将这个形状应用为 View 背景,如下所示:

<View
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:padding="16dp"
    android:background="@drawable/custom_shape" />

形状的确切细节与这个问题无关——只要说我有一个形状就足够了。现在,我不想对形状的各种参数进行硬编码。我们以thicknessRatio为例举个例子。如果我希望厚度比根据屏幕配置而改变,我当然会使用如下整数资源。我会有一个包含以下内容的 values.xml:

<item name="thickness_ratio" type="integer" format="integer">56</item>

然后,android:thicknessRatio="@integer/thickness_ratio" .

到目前为止,还不错。现在,我还想要我的可绘制形状有两种“ flavor ”——“大”一种和“小”一种,我希望查询厚度比,而不是根据配置,而是根据应用的样式到 View 。这就是样式和主题的用途。所以,这是我尝试过的:

第 1 步:声明厚度比的属性 (attrs.xml):

<resources>
        <attr name="thicknessRatio" format="reference" />
</resources>

第 2 步:使用此属性 (styles,xml) 声明两个样式:

<style name="CustomShapeSmall">
    <item name="thicknessRatio">@integer/thickness_ratio_small</item>
</style>

<style name="CustomShapeLarge">
    <item name="thicknessRatio">@integer/thickness_ratio_large</item>
</style>

第 3 步:在 values.xml 中定义两个整数:

<item name="thickness_ratio_small" type="integer" format="integer">32</item>
<item name="thickness_ratio_large" type="integer" format="integer">56</item>

第四步:查询Shape Drawable中的自定义属性:

android:thicknessRatio="?attr/thicknessRatio"

第 5 步:将所需样式应用于 View :

<View
    android:layout_width="96dp"
    android:layout_height="96dp"
    android:padding="16dp"
    style="@style/CustomShapeSmall"
    android:background="@drawable/custom_shape" />

不幸的是,这不起作用。我得到以下异常:

Caused by: android.content.res.Resources$NotFoundException: File res/drawable/custom_shape.xml from drawable resource ID #0x7f020000
    at android.content.res.Resources.loadDrawable(Resources.java:2091)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
    at android.view.View.<init>(View.java:3364)
    at android.view.View.<init>(View.java:3293)
    ... 28 more
Caused by: java.lang.NumberFormatException: Invalid float: "?2130771969"
    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
    at java.lang.StringToReal.parseFloat(StringToReal.java:310)
    at java.lang.Float.parseFloat(Float.java:300)
    at android.content.res.TypedArray.getFloat(TypedArray.java:287)
    at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:827)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:901)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:837)
    at android.content.res.Resources.loadDrawable(Resources.java:2087)

从异常堆栈跟踪来看,android:thicknessRatio="?attr/thicknessRatio" 行似乎解析为问号后跟 R.attr.thicknessRatio 的整数值- 但是没有执行进一步的解析来实际查询此属性的值。

我在这里错过了什么?


编辑:

Here是这个问题的完整github项目。

最佳答案

显然,不可能在 xml 可绘制对象中正确引用属性。

解决方法是创建不同的可绘制对象(一个比例较小,一个比例较大)并在您的不同样式中使用它们。然后您可以将这些样式应用到您的 View 中。

我已经向您发送了一个证明这一点的拉取请求:

https://github.com/curioustechizen/so-question-android-custom-styles/pull/1

另见:

https://stackoverflow.com/a/13471695/1369222

关于android - Shape Drawable 参数取决于应用的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20140174/

有关android - Shape Drawable 参数取决于应用的样式的更多相关文章

  1. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  2. ruby - 如何使用文字标量样式在 YAML 中转储字符串? - 2

    我有一大串格式化数据(例如JSON),我想使用Psychinruby​​同时保留格式转储到YAML。基本上,我希望JSON使用literalstyle出现在YAML中:---json:|{"page":1,"results":["item","another"],"total_pages":0}但是,当我使用YAML.dump时,它不使用文字样式。我得到这样的东西:---json:!"{\n\"page\":1,\n\"results\":[\n\"item\",\"another\"\n],\n\"total_pages\":0\n}\n"我如何告诉Psych以想要的样式转储标量?解

  3. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  4. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  7. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  8. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  9. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  10. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

随机推荐