Android中Style继承

看Android系统Style.xml时,发现有些style是这么写的:

<style name="Animation.DeviceDefault.Activity" parent="Animation.Material.Activity"/>

我们知道style的继承写法即可以使用点,也可以使用parent关键字限定,但这种即有.又有parent关键字限定的到底继承谁呢,先不说结论,先看一段代码:

<style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:backgroundDimAmount">0.9</item>
        <item name="android:scrollbarFadeDuration">251</item>
</style>
<style name="AppTheme_MaterialY" parent="android:Theme.Material.Light"><!-- 这里是下划线 -->
        <item name="android:colorPrimary">@color/quantum_panel_text_color</item>
        <item name="android:colorPrimaryDark">@color/widget_text_panel</item>
        <item name="android:colorAccent">@color/outline_color</item>
        
        <item name="android:backgroundDimAmount">0.7</item>
</style>
<style name="AppTheme.MaterialX" parent="@style/AppTheme_MaterialY">
        <!-- <item name="android:backgroundDimAmount">0.8</item> -->
</style>


大家先可以试下,然后用Java代码中去动态获取Style中各个对应的属性值:

TypedArray typedArr = getTheme().obtainStyledAttributes(R.style.AppTheme_MaterialX,
				new int[] { 0x01010032, 0x010102a8 });<span style="white-space:pre">	</span>// 此处将ids写死,这个是系统在public.xml中定义好的,所以可以这么写,各个系统可能不同
Log.d("wxd_debug", "some float value:" + typedArr.getFloat(0, 0.2f) + ", int value:" 
				+ typedArr.getInt(1, 250));


运行后发现结果为0.7和250,表示没有取到AppTheme中设定的251,将parent去掉,可以取到251的值,因此可以得出结论,当即有.继承又有parent指定继承,parent的继承是优先的。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章