es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

¿Cómo creamos un AttributeSet en Android sin xml?

Sé que podemos establecer AttributeSet sin crear atributos personalizados en XML:

XmlPullParser parser = resources.getXml(myResource);
AttributeSet attributes = Xml.asAttributeSet(parser);

Quiero hacer algo como esto

kotlin

val attrs: AttributeSet = AnArray("android:orientation=Horizontal","
android:layout_height=match_parent")

java

AttributeSet attrs = new AnArray("android:orientation=Horizontal","
android:layout_height=match_parent")

¿Es posible escribir un arreglo y almacenarlo como AttributeSet?

Tags:  , , ,

Answer

  1. Avatar for davy.ai

    Sí, es posible crear un array y almacenarlo como AttributeSet tanto en Kotlin como en Java. Sin embargo, la sintaxis para crear un array en Kotlin y Java es ligeramente diferente. Así es como puedes hacerlo en ambos lenguajes:

    En Kotlin:

    val attrs = arrayOf(
            Pair("android:orientation", "horizontal"),
            Pair("android:layout_height", "match_parent")
    ).toTypedArray()
    
    val attributeSet = AttributeSet(attrs)
    

    En Java:

    AttributeSet attributeSet = new AttributeSet(
            new AttributeSet.Attribute[]{
                    new AttributeSet.Attribute(
                            "android:orientation", "horizontal"),
                    new AttributeSet.Attribute(
                            "android:layout_height", "match_parent")
            }
    );
    

    Ten en cuenta que en ambos casos, creamos un array de atributos como pares clave-valor, y luego pasamos ese array al constructor AttributeSet.

    También ten en cuenta que AttributeSet es una clase personalizada que tendrías que definir tú mismo (ya que no es parte de las bibliotecas estándar de Java o Kotlin). Aquí tienes un ejemplo de implementación de la clase AttributeSet en Kotlin:

    class AttributeSet(private val attributes: Array<><string, string="">>) {
        fun getAttributeCount(): Int {
            return attributes.size
        }
    
        fun getAttributeName(index: Int): String {
            return attributes[index].first
        }
    
        fun getAttributeValue(index: Int): String {
            return attributes[index].second
        }
    }
    

    Y en Java:

    class AttributeSet {
        private Attribute[] attributes;
    
        public AttributeSet(Attribute[] attributes) {
            this.attributes = attributes;
        }
    
        public int getAttributeCount() {
            return attributes.length;
        }
    
        public String getAttributeName(int index) {
            return attributes[index].name;
        }
    
        public String getAttributeValue(int index) {
            return attributes[index].value;
        }
    
        static class Attribute {
            String name;
            String value;
    
            public Attribute(String name, String value) {
                this.name = name;
                this.value = value;
            }
        }
    }
    

    Esta es solo una implementación simple con fines de demostración; probablemente quisieras agregar más funcionalidad a la clase AttributeSet dependiendo de tus necesidades.</string,>

Comments are closed.