Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

254
Views
Cómo cambiar el color de una forma de fondo mediante programación

Tengo una vista personalizada donde tengo que cambiar el color de la forma de fondo del código según la entrada.

La forma:

 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="64dp" android:height="64dp" /> <solid android:color="#BBBBBB" /> </shape>

La vista personalizada:

 public class MyCustomView extends TextView { public MyCustomView(Context context) { super(context); } public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); } public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public void setCustomValue(long customValue) { this.setBackgroundResource(R.drawable.shape_background); this.setTextColor(getColor(R.color.colorCustomViewText)); this.setGravity(Gravity.CENTER); this.setText(String.valueOf(customValue)); } public long getCustomValue() { return Long.parseLong(this.getText().toString()); } private int getColor(int colorId) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return getContext().getColor(colorId); } else { return getContext().getResources().getColor(colorId); } } }

El color de la forma siempre #BBBBBB . No puedo cambiarlo, por ejemplo, a red , independientemente del atributo relacionado con el color que pruebe. Tengo que cambiar el color según la entrada.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede establecer un filtro de color para su forma dibujable:

 Drawable background = getContext().getResources().getDrawable(R.drawable.shape_background); background.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); this.setBackground(background);
over 4 years ago · Santiago Trujillo Report

0

Puede cambiar el color usando GradientDrawable Api,

xml,

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/outerRectangle"> <shape android:shape="oval" > <solid android:color="#FCD366" /> <stroke android:width="1dp" android:color="@android:color/darker_gray" /> </shape> </item>

en Java,

 Drawable tempDrawable = getResources().getDrawable(R.drawable.library_cirecle); LayerDrawable bubble = (LayerDrawable) tempDrawable; (cast to root element in xml) GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle); solidColor.setColor(newColorCode); imageView.setImageDrawable(tempDrawable);

Para obtener más información, consulte el siguiente enlace,

Enlace1 Enlace2

over 4 years ago · Santiago Trujillo Report

0

Pruebe el siguiente código, coloque este código en su clase de vista de texto personalizada y cambie el color de fondo según sus requisitos.

 GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{ContextCompat.getColor(this,R.color.red_500), ContextCompat.getColor(this,R.color.red_500), ContextCompat.getColor(this,R.color.red_500)}); gd.setShape(GradientDrawable.RECTANGLE); gd.setStroke((int)0.5, ContextCompat.getColor(this, R.color.black)); gd.setCornerRadius(8f); gd.setBounds(2, 2, 2, 2); btnCreateUser.setBackground(gd);

Aquí hay una captura de pantalla

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!