Cómo programar ronda esquinas y establecer colores de fondo aleatorios
Me gustaría rodear las esquinas de una vista y también cambiar el color de la vista basada en el contenido en tiempo de ejecución.
TextView v = new TextView(context); v.setText(tagsList.get(i)); if(i%2 == 0){ v.setBackgroundColor(Color.RED); }else{ v.setBackgroundColor(Color.BLUE); } v.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); v.setPadding(twoDP, twoDP, twoDP, twoDP); v.setBackgroundResource(R.drawable.tags_rounded_corners);
Yo estaba esperando establecer un dibujable y el color se superponen, pero no lo hacen. Cualquiera que ejecute segundo es el fondo resultante.
- ¿Cómo se restablece el servicio de WhatsApp incluso si debo detener la aplicación?
- Reproducir video de Youtube en segundo plano usando el reproductor de youtube api
- Repetición de mapa de bits + esquinas redondeadas
- ¿Cómo puedo configurar el fondo de mi forma en xml?
- Animación de fondo en Android?
¿Hay alguna forma de crear esta vista mediante programación, teniendo en cuenta que el color de fondo no se decidirá hasta el tiempo de ejecución?
Editar: Sólo estoy intercambiando entre rojo y azul ahora para la prueba. Posteriormente el color será elegible por el usuario.
editar:
Tags_rounded_corners.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:bottomRightRadius="2dp" android:bottomLeftRadius="2dp" android:topLeftRadius="2dp" android:topRightRadius="2dp"/> </shape>
- Android Layer-List VectorDrawable tamaño
- Notificación del temporizador de cuenta regresiva
- Selector de fondo de botones
- Fondo transparente en CardView - Android
- Imagen de fondo de Android Widget Cambiar
- Aplicar filtro de color en el botón
- Android: establecer el fondo del diseño usando el path de la imagen
- Cambiar el botón androide: fondo a diferentes dibujable
En lugar de setBackgroundColor
, recuperar el fondo dibujable y establecer su color:
v.setBackgroundResource(R.drawable.tags_rounded_corners); GradientDrawable drawable = (GradientDrawable) v.getBackground(); if (i % 2 == 0) { drawable.setColor(Color.RED); } else { drawable.setColor(Color.BLUE); }
Además, puede definir el relleno dentro de sus tags_rounded_corners.xml
:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="4dp" /> <padding android:top="2dp" android:left="2dp" android:bottom="2dp" android:right="2dp" /> </shape>
Enfoque programático total para establecer esquinas redondeadas y agregar color de fondo aleatorio a una vista. No he probado el código, pero tienes la idea.
GradientDrawable shape = new GradientDrawable(); shape.setCornerRadius( 8 ); // add some color // You can add your random color generator here // and set color if (i % 2 == 0) { shape.setColor(Color.RED); } else { shape.setColor(Color.BLUE); } // now find your view and add background to it View view = (LinearLayout) findViewById( R.id.my_view ); view.setBackground(shape);
Aquí estamos usando gradiente dibujable para que podamos hacer uso de GradientDrawable#setCornerRadius
porque ShapeDrawable
NO proporciona tal método.
Creo que la forma más rápida de hacer esto es:
GradientDrawable gradientDrawable = new GradientDrawable( GradientDrawable.Orientation.TOP_BOTTOM, //set a gradient direction new int[] {0xFF757775,0xFF151515}); //set the color of gradient gradientDrawable.setCornerRadius(10f); //set corner radius //Apply background to your view View view = (RelativeLayout) findViewById( R.id.my_view ); if(Build.VERSION.SDK_INT>=16) view.setBackground(gradientDrawable); else view.setBackgroundDrawable(gradientDrawable);
Si no está teniendo una apoplejía puede utilizar
colorDrawable = resources.getDrawable(R.drawable.x_sd_circle); colorDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
Pero esto también cambiará el color del trazo
- SecurityException: el uid del llamador XXXX es diferente al uid del autenticador
- Envío de mensajes a través de WhatsApp