如果视图界面风格需要统一的规划,就需要使用android视图技术中的style。这类似HTML技术和CSS技术的关系。
示例改编自简单使用SimpleCursorAdapter。示例截图如下:
这里将标题字体放大,并且加粗。如果不用style可以这样写:
- <TextView android:id="@+id/riverName" android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textSize="25sp" android:textStyle="bold" />
复制代码
这样的缺点是在众多布局文件中要写很多重复的代码,而且修改的时候也会造成麻烦。style的做法,是将这些style内容写到单独的xml文件中,放置在res/values目录下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="itemTitle">
- <item name="android:textSize">25sp</item>
- <item name="android:textStyle">bold</item>
- </style>
- </resources>
复制代码
在布局文件中的引用:
- <TextView android:id="@+id/riverName" android:layout_width="match_parent"
- android:layout_height="wrap_content" style="@style/itemTitle" />
复制代码
|