A style is reusable set of view attributes. It helps to define the appearance of the android views (colors, fonts etc). It is generally defined as XML file in res/values directory. Style can be for one or more views (generally defined in the xml file within the layout), or can be used to define theme for entire activity or application. The name of xml file for style can be arbitrary but it must end with .xml . The root node of the XML file must be <resources> tag. The <style> tag contain the unique name of the defined style. Various styles attributes are set using <item> tag, which contains a name and the associated value.
MainActivity.java
activity_main.xml
package com.zyasin.style; import android.os.Bundle; import android.app.Activity; public class MainActivity extends Activity { TextView tv ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"/> <TextView android:id="@+id/tv" style="@style/FontStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview style example at www.zyasin.com" /> </LinearLayout>
Place the style definition within the res/values/styles.xml.
<resources> <style name="FontStyle"/> <item name="android:textColor">#C80000</item> <item name="android:textSize"> 40sp </item> <item name="android:typeFace"> serif </item> </style> </resources>