Android : Créer des styles
On peut créer des styles dans le fichiers themes.xml
1.themes.xml
<style name="monTexteStyle">
<item name="android:textColor">#9C27B0</item>
<item name="android:textSize">20dp</item>
</style>
<style name="layoutStyle">
<item name="android:background">#E91E63</item>
<item name="android:padding">10dp</item>
</style>
2. Appliquer le style sur un objet Graphique
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
style="@style/layoutStyle">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
style="@style/monTexteStyle"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"
android:backgroundTint="#FFEB3B"/>
</LinearLayout>
3.ajouter un border,raduis...
Créer un style(monStyle.xml) dans le dossier drawable
monStyle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#fff"
android:centerColor="#000"
android:endColor="#fff"
android:angle="270" />
<corners android:radius="5dp" />
<stroke android:width="20px" android:color="#009688" />
</shape>
Appeler le style monStyle.xml dans un objet graphique
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/monstyle">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"
style="@style/monTexteStyle"/>
</LinearLayout>