目前在研究vlc for android的代码,对于其中的主题属性有了一些了解,记录如下以便以后查找。
vlc-android\res\values\styles.xml中有如下定义:
<style name="Theme.VLC" parent="Theme.VLC.7"/>
<style name="Theme.VLC.7" parent="Theme.VLC.Apearance">
<item name="actionBarStyle">@style/ActionBar</item>
<item name="windowActionBarOverlay">true</item>
<item name="drawerArrowStyle">@style/ActionBar.ArrowToggle</item>
</style>
<style name="Theme.VLC.Apearance" parent="Theme.AppCompat.Light.NoActionBar">
<item name="marginTopContent">50dp</item>
......
<item name="ic_pause">@drawable/ic_pause</item>
......
</style>
我们可以看到vlc-android\res\drawable\下面有一个ic_pause.xml,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_pause_normal" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/ic_pause_pressed" android:state_focused="true"/>
<item android:drawable="@drawable/ic_pause_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/ic_pause_normal"/>
</selector>
因此属性ic_pause其实是一个正常状态和按下状态的暂停图片,这些图片ic_pause_normal.png及ic_pause_pressed.png存在于res的drawable-hdpi、drawable-ldpi、drawable-mdpi、drawable-xhdpi目录下。
其对应的属性定义在vlc-android\res\values\attrs.xml中:
<attr name="ic_pause" format="reference" />
<ImageButton
android:id="@+id/header_play_pause"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:layout_marginRight="@dimen/default_margin"
android:background="#00000000"
android:contentDescription="@string/pause"
android:focusable="true"
android:scaleType="fitXY"
android:src="?attr/ic_pause"
android:nextFocusForward="@+id/header_play_pause"
android:nextFocusUp="@+id/ml_menu_search"
android:nextFocusDown="@id/header_play_pause"
android:nextFocusLeft="@id/header_play_pause"
android:nextFocusRight="@id/header_play_pause" />
这样就相当于使用了主题中的ic_pause属性,实际就是不同状态下引用哪个图片作为图片按钮的当前图片。
最后别忘了AndroidManifest.xml中给对应的Activity设置主题,如下所示:
<activity
android:name=".gui.MainActivity"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustPan"
android:theme="@style/Theme.VLC" >
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
如果不设置该Activity的android:theme,运行时将会出现“android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path)”这种错误。