【问题标题】:How can i keep the borders on a view while changing dynamically its background color?如何在动态更改其背景颜色的同时保持视图的边框?
【发布时间】:2013-02-21 02:53:08
【问题描述】:

我在线性布局上应用了边框:

<LinearLayout
        android:id="@+id/lay1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="80"
        android:background="@drawable/linearborder2" />

在我的主要活动中,我想动态更改此活动的背景颜色,但我这样做是为了让边框逐渐消失。 那么我该怎么做才能保留这些边框并更改颜色呢?

谢谢

【问题讨论】:

  • 好吧..使用另一个带边框的drawable..!
  • 怎么做?在我的主要活动中,我写了 vi.setBackgroundColor(color) color 是选择的新颜色
  • 你应该创建另一个drawable..就像“linearborder2”
  • linearborder2 是一个 xml 文件,我如何在 xml 文件中放入一个动态变量(即颜色)?

标签: android layout colors background


【解决方案1】:

假设linearborder2 是可绘制的形状。您可以动态更改可绘制形状的颜色。 This 应该对此有所帮助。

【讨论】:

  • 我试过这个:GradientDrawable sd = (GradientDrawable) tv2.getBackground().mutate(); sd.setColor(颜色); sd.invalidateSelf(); &这个我在你给我的帖子上找到的 ShapeDrawable bgShape = (ShapeDrawable )btn.getBackground(); bgShape.getPaint().setColor(Color);但我在两者上都有一个空异常指针
猜你喜欢
  • 1970-01-01
  • 2018-02-04
  • 2010-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多