您的位置:首页 > Web前端

如何为PreferenceScreen分别设置窗口背景图片和actionBar背景图片/拖动时不黑屏

2015-06-10 21:09 771 查看
先看下需要的效果图:



PreferenceScreen里,actionBar是深色木质纹理贴图,内容是浅色木质纹理贴图。

步骤如下:

1. 打开values目录下的styles.xml(没有就新建一个),添加内容如下:

<pre name="code" class="html"><pre name="code" class="html"> <style name="McpreActivityStyle" parent="android:Theme.Holo.Light">
<item name="android:windowBackground">@drawable/wood02</item>
<item name="android:textColor">#ff000000</item>
<item name="android:actionBarStyle">@style/WoodActionBarStyle</item>
</style>

<style name="MyActionBarTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title"> <item name="android:textColor">#fff</item> </style>
<style
name="WoodActionBarStyle" parent="android:Widget.ActionBar">
<item name="android:titleTextStyle">@style/MyActionBarTitleText</item>
<item name="android:background">@drawable/wood029</item>

</style>

<style name="MyActionBarTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#fff</item>
</style>





style嵌套层数有点多...习惯就好。

McpreActivityStyle最关键的是parent="android:Theme.Holo.Light",这个系统自带主题允许自定义标题栏和窗口背景,其他主题可能已经固定了背景无法修改;不继承系统自带主题则需要添加的属性太多才能达到预期效果。

第三行<item name="android:actionBarStyle">@style/WoodActionBarStyle</item>重定义了actionBar的背景和字颜色,不添加该行则android:windowBackground的图片将填满整个PreferenceScreen。

2. 在manifest.xml中的相应activity中配置,如:

<activity android:name=".MCPreActivity"
android:label="速度首选项"
android:logo="@drawable/luckystar_kagami02"
android:theme="@style/McpreActivityStyle" >
<intent-filter >
<action android:name="com.boweng.mecanumcontrol.MCPreActivity" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

3. 在上面的例子里PreferenceScreen是用XML定义的,如下:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory android:title="线速度" >
<ListPreference
android:defaultValue="200"
android:dialogTitle="Y轴速度"
android:entries="@array/vspeedstring"
android:entryValues="@array/vspeedvalues"
android:key="Vspeed"
android:summary="选择一个速度"
android:title="Y轴速度"
android:negativeButtonText="取消" />
<ListPreference
android:defaultValue="200"
android:dialogTitle="X轴速度"
android:entries="@array/hspeedstring"
android:entryValues="@array/hspeedvalues"
android:key="Hspeed"
android:summary="选择一个速度"
android:title="X轴速度"
android:negativeButtonText="取消" />
<ListPreference
android:defaultValue="200"
android:dialogTitle="十字键最大限速"
android:entries="@array/tenspeedstring"
android:entryValues="@array/tenspeedvalues"
android:key="Tenspeed"
android:summary="选择一个速度"
android:title="十字键最大限速"
android:negativeButtonText="取消" />

</PreferenceCategory>
<PreferenceCategory android:title="角速度" >
<ListPreference
android:defaultValue="150"
android:dialogTitle="自转角速度"
android:entries="@array/rspeedstring"
android:entryValues="@array/rspeedvalues"
android:key="Rspeed"
android:summary="选择一个速度"
android:title="自转角速度"
android:negativeButtonText="取消" />
</PreferenceCategory>

</PreferenceScreen>

题外

之前,在网上找的代码,效果不好。

主要有两个问题:一是背景图片覆盖了整个窗口,actionBar无法单独设置;二是没有继承系统自带主题,后期需要手动添加的属性太多。。。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="sys_set_preacitivity">
<item name="android:windowBackground">@drawable/background</item>
<item name="android:focusable">false</item>
<item name="android:textColor">#ff000000</item>
<item name="android:cacheColorHint">#ffffffff</item>
<item name="android:windowNoTitle">false</item>  <!-- 用来设定是否显示标题  -->
</style>
</resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息