vlc for android 主题属性分析

本文详细解读了VLC for Android中主题属性的定义与使用方法,包括样式、图标资源的配置以及如何在Activity中应用主题,避免资源加载错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目前在研究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" />



那么怎么使用呢?见vlc-android\res\layout\audio_player.xml中:

<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)”这种错误。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值