【问题标题】:How do you use Android R Drawables without using XML?如何在不使用 XML 的情况下使用 Android R Drawables?
【发布时间】:2012-09-07 16:04:06
【问题描述】:

http://androiddrawableexplorer.appspot.com/ 我希望能够在我的项目中使用这些可绘制对象,但我的项目是在运行时生成的。我只使用 XML 来定义新的活动,因为坦率地说,我讨厌在 java 中使用 XML。我正在寻找与此类似的方法。一种不使用任何 XML 来“findByID”它需要的组件。谢谢你。 Drawable foo = new Drawable(bar);

【问题讨论】:

  • “我的项目是在运行时生成的”是什么意思?
  • 您使用 Button derp = new Button(context); 而不是使用 FindById (Button);或者...而不是 setContentView(R.some.thing) 你使用 setContentView(new Scrollview(....));

标签: android resources system drawable r.java-file


【解决方案1】:

你可以用这个:

Drawable d = getResources().getDrawable(id)

【讨论】:

    【解决方案2】:

    那么,您到底在寻找什么?您想在没有 XML 的情况下在运行时构建活动吗?您只想能够操纵 android 可绘制对象吗?

    这就是你使用 android drawables 的方式:

    How to use default Android drawables

    【讨论】:

    • @Pangamma - 如果其中一个答案解决了您的问题,最好将答案标记为解决方案(通过单击答案旁边的复选标记)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 2017-04-11
    • 2013-09-30
    相关资源
    最近更新 更多