【问题标题】:Change XML shape color while app is running在应用程序运行时更改 XML 形状颜色
【发布时间】:2012-05-04 20:02:40
【问题描述】:

是否可以在应用程序运行时更改 Java 代码中的矩形(以 xml 绘制)颜色?

我的矩形.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
    <stroke android:width="2dp" android:color="#ffffff" />
    <padding android:left="20dp"
        android:top="20dp"
        android:right="20dp"
        android:bottom="20dp" />

    <solid android:color="#006600" />
</shape>

由 main.xml 绘制:

<View
    android:id="@+id/myRectangleView"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:background="@drawable/rectangle"/>

我试过这种方式:

   GradientDrawable sd;
    View viewrectangle;
    viewrectangle = (View) findViewById(R.id.myRectangleView);
    sd = (GradientDrawable) viewrectangle.getBackground();
    sd.setColor(0xffffff00);
    sd.invalidateSelf();

只有当我把它放在 OnCreate 方法中时它才有效。

我想通过一个按钮改变矩形颜色,所以我把这段代码放在按钮的 onClick() 方法中。但是当我在应用程序运行时单击按钮时,颜色不会改变。有什么建议吗?

【问题讨论】:

  • 如果以下任何答案解决了您的问题,或帮助您解决了问题,您应单击该答案旁边的复选标记,将其标记为已接受的答案,并给予作者功劳。

标签: android xml


【解决方案1】:

使用此代码并且它有效,或者考虑使用 viewrectangle.invalidate() 重新绘制 viewrectangle,但它不应该是 nescarry:

View viewrectangle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    viewrectangle = (View) findViewById(R.id.myRectangleView);

}

public void doClick(View v) {
    GradientDrawable sd = (GradientDrawable) viewrectangle.getBackground();
    sd.setColor(0xffffff00);
    sd.invalidateSelf();
}

在本例中,“doClick()”方法设置在 main.xml 中:

<Button android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Button"
        android:onClick="doClick"/>

【讨论】:

    【解决方案2】:

    你可以把这段代码放在一个单独的方法中,那个方法你可以从按钮的onClick调用..

    【讨论】:

      【解决方案3】:

      你可以试试彩色滤光片。我以前用它来更改按钮的颜色(请注意,它们一开始是标准灰色),如果你从另一种颜色开始,它可能是一个非常不同的结果。无论如何,我是如何做到的一个例子:

      导入 PorterDuff 图形材料:

      import android.graphics.PorterDuff;
      

      在类中定义您要进行颜色过滤的项目并设置过滤器:

      Button atdButton = (Button) convertView.findViewById(R.id.attendbutton);
      
          if (atdState[position].equals("P")) {
              atdButton.getBackground().setColorFilter(0xFF00FF00,  // Set filter to green
                      PorterDuff.Mode.MULTIPLY);
          } else if (atdState[position].equals("T")) {
              atdButton.getBackground().setColorFilter(0xFFFFFF00,  // Set filter to yellow
                      PorterDuff.Mode.MULTIPLY);
          } else if (atdState[position].equals("E")) {
              atdButton.getBackground().setColorFilter(0xFFFF6600,  // Set filter to orange
                      PorterDuff.Mode.MULTIPLY);
          } else if (atdState[position].equals("U")) {
              atdButton.getBackground().setColorFilter(0xFFFF0000,  // Set filter to red
                      PorterDuff.Mode.MULTIPLY);
          } else {
              atdButton.getBackground().clearColorFilter();
          }
      

      【讨论】:

        猜你喜欢
        • 2011-08-21
        • 2012-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-14
        相关资源
        最近更新 更多