studio reales proyectos programacion introducción incluye guia fuente desarrollo código avanzado aplicaciones android coding-style resources themes

android - reales - Cómo obtener mi propio valor de atributo definido en mi estilo



manual programacion android (3)

Quiero crear 3 temas diferentes para un diálogo utilizando un atributo personalizado (propio). Me gustaría configurar los colores del título agregando esto al estilo del tema: <item name="titleColor">#FF0000</item>

mis temas.xml:

<?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyTheme" parent="@android:style/Theme"> <item name="android:alertDialogStyle">@style/dialog</item> </style> <style name="MyRedTheme" parent="MyTheme"> <item name="titleColor">#FF0000</item> </style> <style name="MyGreenTheme" parent="MyTheme"> <item name="titleColor">#00FF00</item> </style> <style name="MyBlueTheme" parent="MyTheme"> <item name="titleColor">#0000FF</item> </style>

Definí el atributo titleColor en attrs.xml:

<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyCustomAttributes"> <attr name="titleColor" format="color|reference" /> </declare-styleable> </resources>

Aplico uno de los temas para el dialogo. ¿Cómo puedo pasar el valor de mi atributo titleColor a un atributo "android: color"?

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.dicare" android:shape="rectangle"> <solid android:color="I want to pass titleColor value here"/> </shape>


? titleColor ver aquí

o

Debería definir sus colores en el archivo colors.xml y hacer referencia a ellos como un recurso normal: @ color / MyRed

Debería crear un atributo personalizado para sus propias vistas que desee que se pueda personalizar desde las xmls de diseño. Por ejemplo, puede extender TextView para escribir la primera línea de texto en un color (titleColor) que el resto del texto (android: textColor).

<color name="MyRed">#FF0000</color> <style name="MyRedTheme" parent="MyTheme"> <item name="titleColor">@color/MyRed</item> </style> <shape xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.dicare" android:shape="rectangle"> <solid android:color="@color/MyRed"/> </shape>


Creo que todo lo que necesitas hacer es cambiar el android: color para custom: color:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res/com.dicare" android:shape="rectangle"> <solid custom:color="I want to pass titleColor value here"/> </shape>


Entonces, lo primero que tendrá que hacer es editar su archivo attrs.xml. Aquí agregará todos los atributos que desea definir a través de xml. Aquí hemos agregado un título, así como los botones derecho e izquierdo con texto y un dibujo.

<declare-styleable name="activity_header"> <attr name="title" format="string" /> <attr name="left_button_text" format="string" /> <attr name="left_button_drawable" format="reference" /> <attr name="right_button_text" format="string" /> <attr name="right_button_drawable" format="reference" /> <attr name ="hide_buttons"> <enum name="yes" value="1" /> <enum name="no" value="0" /> </attr> </declare-styleable>

A continuación, querrá crear su diseño. Lo importante aquí es agregar un espacio de nombres que haga referencia a su aplicación. Aquí la he llamado app. Solo debe incluir el nombre de su paquete después de http://schemas.android.com/apk/res/ . Ahora puede usar cualquiera de los atributos que definió anteriormente en su archivo xml.

<

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res/com.biggu.shopsavvy.ui4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <com.biggu.shopsavvy.ui4.ActivityHeader android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/header" app:title = "History" app:left_button_text="Share" app:left_button_drawable="@drawable/ic_menu_share" app:right_button_drawable="@drawable/small_btn_archive" app:right_button_text="Organize" />

Ahora que tenemos nuestros atributos definidos en nuestro archivo xml, necesitamos recuperarlos de nuestro componente personalizado que hemos creado. Simplemente debe obtener los atributos de estilo obtenidos utilizando el recurso que creó, aquí usamos activity_header.

public class ActivityHeader extends LinearLayout { TextView mTitleEditText; Button mLeftButton; Button mRightButton; View mDelimeter; private ViewGroup mAdditionalPanel; public ActivityHeader(Context context, AttributeSet attrs) { super(context, attrs); ViewGroup.inflate(context, R.layout.header , this); findViews(); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.activity_header); if ( a.getInt(R.styleable.activity_header_hide_buttons, 0) == 1) //hide buttons { mLeftButton.setVisibility(GONE); mRightButton.setVisibility(GONE); } else { setLeftButtonDrawable(a.getResourceId(R.styleable.activity_header_left_button_drawable, android.R.drawable.ic_menu_info_details)); setLeftButtonText(a.getString(R.styleable.activity_header_left_button_text)); setRightButtonDrawable(a.getResourceId(R.styleable.activity_header_right_button_drawable, android.R.drawable.ic_menu_info_details)); setRightButtonText(a.getString(R.styleable.activity_header_right_button_text)); } setTitle(a.getString(R.styleable.activity_header_title)); a.recycle(); } }

Eso es. Feliz codificacion