首页 \ 问答 \ Android attr到xml drawables(Android attr to xml drawables)

Android attr到xml drawables(Android attr to xml drawables)

TL; DR我正在寻找public static Drawable getDrawableFromAttribute(Context context, String attrName)


我正在寻找一种方法,加载动态drawable,这是我的风格定义自定义属性。 那是我的配置

attr.xml

<resources>
   <attr name="custom_image" type="reference">
</resources>

styles.xml

<resources>
   <style name="demo">
      <item name="custom_image">@drawable/fancy_picture</item>
   </style>
</resources>

fancy_picture名为/res/drawables/fancy_pictures.xml

现在,我希望有人输入字符串“custom”和“image”, ImageView应该在其中显示fancy_picture。

做这个的最好方式是什么? 如果我使用XML-Layout文件,我可以写

<ImageView
    ...
    android:src="?custom_image"
    ...
    />

我没有在我的样式xml中使用declare-styleable ,如果可能的话我想完全忽略它们。


TL;DR I looking for an implementation of public static Drawable getDrawableFromAttribute(Context context, String attrName).


I'm searching for a way, to load dynamic drawables, which are defined in my style with custom attributes. That's my configuration

attr.xml

<resources>
   <attr name="custom_image" type="reference">
</resources>

styles.xml

<resources>
   <style name="demo">
      <item name="custom_image">@drawable/fancy_picture</item>
   </style>
</resources>

The fancy_picture is a named /res/drawables/fancy_pictures.xml.

Now, I want someone to enter the string "custom" and "image" and the ImageView should show the fancy_picture in it.

What is the best way to do this? If I use a XML-Layout file, I could write

<ImageView
    ...
    android:src="?custom_image"
    ...
    />

I didn't use declare-styleable in my style xml and I would like to ignore them completely, if possible.


原文:https://stackoverflow.com/questions/29654275
更新时间:2023-06-29 18:06

最满意答案

在你的情况下,变量$ fruit将具有值“pear”,而$ color =“green”因为你总是重新定义它

 <?php
 $fruits = array();
 $fruits[] = ["name" => "Apple", "colour"=>"red"];
 $fruits[] = ["name" => "Banana", "colour"=>"yellow"];
 $fruits[] = ["name" => "Pear", "colour"=>"green"];
 ?>

第一种方式:

<?php
include 'fruits.php';
$num=count($fruits);
$i = 0;
while ($i < $num){
echo "<p>My ".$fruits[$i]["name"]." is ".$fruits[$i]["colour"].".</p>";
$i++;
}
?>

第二种方式( 更容易 ):

<?php 
include 'fruits.php';
foreach ($fruits as $fruit){
  echo "<p>My ".$fruit["name"]." is ".$fruit["colour"].".</p>";
}

?>

其中$ fruit是数组$ fruits的元素


in your case, variable $fruit will have a value "pear", and $colour = "green" Because you always redefine it

 <?php
 $fruits = array();
 $fruits[] = ["name" => "Apple", "colour"=>"red"];
 $fruits[] = ["name" => "Banana", "colour"=>"yellow"];
 $fruits[] = ["name" => "Pear", "colour"=>"green"];
 ?>

1st way:

<?php
include 'fruits.php';
$num=count($fruits);
$i = 0;
while ($i < $num){
echo "<p>My ".$fruits[$i]["name"]." is ".$fruits[$i]["colour"].".</p>";
$i++;
}
?>

2nd way (much easier):

<?php 
include 'fruits.php';
foreach ($fruits as $fruit){
  echo "<p>My ".$fruit["name"]." is ".$fruit["colour"].".</p>";
}

?>

where $fruit is element of the array $fruits

相关问答

更多
  • 您缺少PHP Pear DB库。 通过pear install DB安装库 You are missing the PHP Pear DB library. Install the library via pear install DB
  • 我会在root目录中创建一个像common.php这样的文件,它应该包含在所有的php文件中。 在common.php中,我将创建自动加载器,如下例所示: http ://www.php.net/manual/en/function.spl-autoload-register.php,它可以自动加载所需的类,而无需在任何地方包含它们。 I would create a file in root dir like common.php which should be included in all php f ...
  • 在你的情况下,变量$ fruit将具有值“pear”,而$ color =“green”因为你总是重新定义它 "Apple", "colour"=>"red"]; $fruits[] = ["name" => "Banana", "colour"=>"yellow"]; $fruits[] = ["name" => "Pear", "colour"=>"green"]; ?> 第一种方式:
  • 正如Daniel所说,理想情况下,您需要MVC中的一个中心点来处理数据库连接。 通过这种方式,它可以在整个项目中访问,而且您可以轻松跟踪一次运行的连接数(最好只是一个当然)。 如果您正在使用框架,请寻找可以执行这些操作的预制库。 最新的框架有一些东西可以解决这个问题。 如果您只是在本机PHP创建自己的项目,那么您可以实现像Daniel所描述的行为。 As Daniel stated, ideally you want a central point in your MVC that handles your ...
  • DB2许可证注册过程: 在Windows操作系统上,通过输入以下命令注册Db2®许可证证书文件: db2install_path\bin\db2licm -a filename 其中db2install_path是Db2安装路径, filename是与您购买的产品或产品相对应的许可证证书文件的完整路径名和文件名。 在Linux或UNIX操作系统上,通过输入以下命令来注册Db2许可证证书文件: INSTHOME/sqllib/adm/db2licm -a filename 其中INSTHOME表示实例所有者的 ...
  • 您最好创建一个DB类或利用已创建的DB来实现您想要做的事情。 像这样的事情的通常流程称为延迟加载/依赖注入 。 你将所需的对象传递给类的地方。 如果你选择了这个路径,我建议你阅读依赖注入,尽可能多的东西,它有优点和缺点,但在OOP中是必不可少的。 正如Ben在评论中所说 : 依赖注入是OOP的一个关键原则。 我想补充一点,如果你认真对待OOP,你还应该考虑一般的自动加载器 , PSR-4和Composer 。 不在上面提到的一方,你最好看看PHPTheRightWay ,他们列出了很多东西,包括依赖注入 。 ...
  • SomePage.php File Name 的download.php $FileNo=$_GET['FileNO']; //Use Mysql Query to find the 'full path' of file using $FileNo. // I Assume $FilePaths as 'Full File Path'. download_file($FilePaths); fu ...
  • 您可能想要使用其中之一 所有类都可以访问的全局内容(例如全局变量或单例),或者 传递给所有类的实例化的东西,产生你所谓的“软错误” 收集此类错误。 然后,您想要使用您收集的任何内容,并以某种方式将其添加到System类的输出中。 更具体... 这是使用全局解决方案的示例: global $softErrorMessages = array(); class A { function sampleFunctionA() { // [...] // some code setting ...
  • 将内容类型设置为XML,以便浏览器将其识别为XML。 header( "content-type: application/xml; charset=ISO-8859-15" ); 在您的代码中将其更改为: // Set the content type to be XML, so that the browser will recognise it as XML. header( "content-type: application/xml; charset=ISO-8859-15" ); // ...
  • (只是复制我的评论,解决了@ reverendocabron的问题,以便于参考。) $ flags参数应该是SQLITE3_OPEN_READONLY , SQLITE3_OPEN_READWRITE和/或SQLITE3_OPEN_CREATE (其值分别为1,2和4),而你给它一个Unix风格的0666参数。 在你的情况下,我猜你正在寻找的是SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_ ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。