Wie ändere ich die Formfarbe dynamisch?

Lesezeit: 4 Minuten

Wie andere ich die Formfarbe dynamisch
Chobo2

Ich habe

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <solid
       android:color="#FFFF00" />
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
</shape>

<TextView
    android:background="@drawable/test"
    android:layout_height="45dp"
    android:layout_width="100dp"
    android:text="Moderate"
/>

Jetzt möchte ich, dass diese Form die Farbe basierend auf Informationen ändert, die ich von einem Webdienstaufruf zurückerhalte. Es könnte also vielleicht gelb oder grün oder rot oder was auch immer sein, abhängig von der Farbe, die ich vom Webservice-Anruf erhalte.

Wie kann ich die Farbe der Form ändern? Basierend auf diesen Informationen?

  • Wie von der @Couitchy-Methode ernannt View.getBackground() gibt a zurück GradientDrawable und nicht ein ShapeDrawable wodurch die App zur Laufzeit abstürzt, da beim Versuch, die Referenz abzurufen und die Farbe programmgesteuert festzulegen, eine ungültige Umwandlung vorliegt. [Android Shape doc]( developer.android.com/guide/topics/resources/…) Zustände: ZUSAMMENGESTELLTER RESSOURCENDATENTYP: Ressourcenzeiger auf a GradientDrawable.

    – Luis Quijada

    8. Juni 2013 um 15:37 Uhr


  • Mögliches Duplikat von Set android shape color programmatically

    – CJay

    24. April 2019 um 6:35 Uhr

1646022188 406 Wie andere ich die Formfarbe dynamisch
Ronnie

Sie könnten es einfach so ändern

GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);

  • Was ist btn.getBackground?

    – Chobo2

    23. August 2011 um 17:30 Uhr

  • ich bekomme java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ShapeDrawable beim Ausprobieren dieses Vorschlags.

    – prolink007

    15. August 2013 um 18:37 Uhr

  • Funktioniert nicht. Sie erhalten einen Cast-Fehler. Benötigt eine Lösung oder eine andere akzeptierte Antwort

    – ndgrün

    30. Juni 2014 um 17:32 Uhr

  • Das funktioniert gut. Die vorherigen Kommentare sind veraltet, da die Antwort bearbeitet wurde!

    – W3hri

    6. Juli 2015 um 13:05 Uhr


  • sollte GradientDrawable verwenden bgShape = (GradientDrawable)btn.getBackground().getCurrent();

    – Naveed148

    9. September 2016 um 10:52 Uhr


1646022188 270 Wie andere ich die Formfarbe dynamisch
Couchy

Bei mir stürzte es ab, weil getBackground zurückgegeben a GradientDrawable anstelle einer ShapeDrawable.

Also habe ich es so modifiziert:

((GradientDrawable)someView.getBackground()).setColor(someColor);

1646022189 471 Wie andere ich die Formfarbe dynamisch
Manuel v.

Dies funktioniert bei mir mit einer anfänglichen XML-Ressource:

example.setBackgroundResource(R.drawable.myshape);
GradientDrawable gd = (GradientDrawable) example.getBackground().getCurrent();
gd.setColor(Color.parseColor("#000000"));
gd.setCornerRadii(new float[]{30, 30, 30, 30, 0, 0, 30, 30});
gd.setStroke(2, Color.parseColor("#00FFFF"), 5, 6);

Ergebnis von oben: http://i.stack.imgur.com/hKUR7.png

  • Dies ist die richtige Antwort. Beide oben genannten Antworten haben bei mir nicht funktioniert. Ich bekomme Ausnahme auf beides.

    – Sanal Varghese

    10. August 2017 um 7:49 Uhr

Sie können Ihre eigenen Formen in Java erstellen. Ich habe das für ein iPhone wie Page Controler gemacht und die Formen in Java gemalt:

/**
 * Builds the active and inactive shapes / drawables for the page control
 */
private void makeShapes() {

    activeDrawable = new ShapeDrawable();
    inactiveDrawable = new ShapeDrawable();
    activeDrawable.setBounds(0, 0, (int) mIndicatorSize,
            (int) mIndicatorSize);
    inactiveDrawable.setBounds(0, 0, (int) mIndicatorSize,
            (int) mIndicatorSize);

    int i[] = new int[2];
    i[0] = android.R.attr.textColorSecondary;
    i[1] = android.R.attr.textColorSecondaryInverse;
    TypedArray a = this.getTheme().obtainStyledAttributes(i);

    Shape s1 = new OvalShape();
    s1.resize(mIndicatorSize, mIndicatorSize);
    Shape s2 = new OvalShape();
    s2.resize(mIndicatorSize, mIndicatorSize);

    ((ShapeDrawable) activeDrawable).getPaint().setColor(
            a.getColor(0, Color.DKGRAY));
    ((ShapeDrawable) inactiveDrawable).getPaint().setColor(
            a.getColor(1, Color.LTGRAY));

    ((ShapeDrawable) activeDrawable).setShape(s1);
    ((ShapeDrawable) inactiveDrawable).setShape(s2);
}

hoffe das hilft. Grüß Fabian

1646022190 217 Wie andere ich die Formfarbe dynamisch
nilsi

Vielleicht muss jemand anders die Farbe im XML ändern, ohne mehrere drawables zu erstellen, wie ich es brauchte. Machen Sie dann einen Kreis ohne Farbe zeichenbar und geben Sie dann backgroundTint für die ImageView an.

Kreis.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
</shape>

Und in deinem Layout:

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/circle"
    android:backgroundTint="@color/red"/>

Bearbeiten:

Es gibt einen Fehler in Bezug auf diese Methode, der verhindert, dass sie auf Android Lollipop 5.0 (API-Ebene 21) funktioniert. Wurden aber in neueren Versionen behoben.

    LayerDrawable bgDrawable = (LayerDrawable) button.getBackground();
    final GradientDrawable shape = (GradientDrawable)
            bgDrawable.findDrawableByLayerId(R.id.round_button_shape);
    shape.setColor(Color.BLACK);

1646022190 235 Wie andere ich die Formfarbe dynamisch
A. Schukri

circle.xml (zeichnbar)

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid
    android:color="#000"/>

<size
    android:width="10dp"
    android:height="10dp"/>
</shape>

Layout

<ImageView
      android:id="@+id/circleColor"
      android:layout_width="15dp"
       android:layout_height="15dp"
      android:textSize="12dp"
       android:layout_gravity="center"
       android:layout_marginLeft="10dp"
      android:background="@drawable/circle"/>

in Aktivität

   circleColor = (ImageView) view.findViewById(R.id.circleColor);
   int color = Color.parseColor("#00FFFF");
   ((GradientDrawable)circleColor.getBackground()).setColor(color);

  • Nicht perfekt, aber hat mir geholfen, die Lösung zu finden.

    – Rakesh Yadav

    5. Mai 2018 um 10:22 Uhr

884080cookie-checkWie ändere ich die Formfarbe dynamisch?

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy