UI(三) 尽量使用style

    技术2025-09-11  14

    一、概述

        style可以用来描述一个View或Window的外观,style用一个独立的XML(res/values)来定义。用style抽取出与外观相关的描述,可以将设计和内容独立开来。

    三、实例

    【效果图】

         下图的第一行文字Style为CodeFont,第二行文字为CodeFont.web。style之间还可以存在继承关系。例如已经定义style A ,再定义style B时,B可以通过继承A来获得A的属性,并且再定义自己的特性。实例中CodeFont.web继承CodeFont,并且具有自动连接Web地址的功能。

    【代码要点】(注意:以下代码被自动加上了mce节点应该是编辑插件的Bug引起的。) style.xml:在res/values中新建style.xml,定义CodeFont和CodeFont.web两种style。<?xml version="1.0" encoding="utf-8"?> <resources> <mce:style name="CodeFont" parent="@android:style/TextAppearance.Medium"><!-- <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_gravity">center_horizontal</item> <item name="android:textColor">#00FF00</item> <item name="android:typeface">monospace</item> --></mce:style><style name="CodeFont" parent="@android:style/TextAppearance.Medium" mce_bogus="1"> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_gravity">center_horizontal</item> <item name="android:textColor">#00FF00</item> <item name="android:typeface">monospace</item> </style> <mce:style name="CodeFont.web"><!-- <item name="android:autoLink">web</item> --></mce:style><style name="CodeFont.web" mce_bogus="1"> <item name="android:autoLink">web</item> </style> </resources>  

    main.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text="@string/hello" style="@style/CodeFont" mce_style="@style/CodeFont" /> <TextView android:text="http://www.csdn.net/" style="@style/CodeFont.web" mce_style="@style/CodeFont.web" /> </LinearLayout>  

    注意:继承关系包括两种,其一是继承Android平台已经定义好的Style(Android Platform Style),另一种是继承自己定义好的Style. 在Style的定义上略有不同.

    最新回复(0)