android 主题样式theme style区别

有时候这两个东西还真是会搞混了,记录一下:

1.主题应用于Activity或者整个Application,而style通常是应用于一个控件

2.主题和样式都是一堆属性的集合。区别在于,主题可以把一个控件的整个样式看成它的一个属性,如 ,只要包含在这个主题下的TextView,都会应用这个主题。而样式之针对具体的一个控件。因为主题和样式都可以定义在<style>中,所以很容易搞混了。

比如,定义了一个名为NumberPicker的控件,这个部分为属性定义

<resources>
<attr name="numberPickerStyle" format="reference" />

    <declare-styleable name="NumberPicker">
        <attr name="solidColor" format="color|reference" />
        <attr name="selectionDivider" format="reference" />
        <attr name="selectionDividerHeight" format="dimension" />
        <attr name="selectionDividersDistance" format="dimension" />
        <attr name="internalMinHeight" format="dimension" />
        <attr name="internalMaxHeight" format="dimension" />
        <attr name="internalMinWidth" format="dimension" />
        <attr name="internalMaxWidth" format="dimension" />
        <attr name="internalLayout" format="reference" />
        <attr name="virtualButtonPressedDrawable" format="reference"/>
        <attr name="enableSectionDivider" format="boolean"/>
		<attr name="rcNumberTextColor" format="color|reference"/>
		<attr name="selectedNumberTextColor" format="color|reference"/>
        <attr name="indicatorLayout" format="reference"/>
    </declare-styleable>
</resources>

如果要定义这个控件的属性不用多说,下面主要看怎么控制整个Activity或者属性的:

style的定义:

<style name="Theme.hehe" parent="@style/Theme.heihei">
        <item name="numberPickerStyle">@style/theNumberPickerStyle</item>
    </style>


Manifest中:

<activity
            android:name="com.xxx"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.hehe" />

这样,只要在com.xxx Activity中的numberPicker,就会应用上面theNumberPickerStyle的属性~

不明白就留言吧,我偷懒了。。。



版权声明:本文为yulinyige原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。