改善用户体验

本文深入探讨了Android中style和theme的使用方法,包括如何自定义和继承style及theme,以及如何在UI元素中应用样式和主题。此外,文章还介绍了如何利用selector创建动态背景效果,覆盖不同状态下的UI元素样式变化。

Android之style和theme的使用

为了提升用户体验,手机客户端都做得布局合理而且美观.......Android的Style设计就是提升用户体验的关键之一。首先,style和theme都是资源,android提供了很多这样的默认资源。你可以来使用它们。同时你也可以自己定义style和theme。这非常的简单,只需要在res/values/这个路径里面新建一个.xml文件,而且他的根节点必须是<resources>.对每一个style和theme,给<style>element增加一个全局唯一的名字,也可以选择增加一个父类属性,我们写的style和theme就会继承这个父类的属性。style和theme的定义格式相同。不过style是针对view来说的,比如TextView,EditText这些,而theme必须针对整个activity或者整个程序,你必须在AndroidManifest.xml中的<application>或者<activity>中定义。
先来看看style,比如如下一段代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>
可以看到这个style的名字为CodeFont。 parent后面就是父类的style, CodeFont继承这个父类的属性。可以看到这个父类的style是android中默认的,你也可以继承你自定义的style,这时候不需要再写parent属性,而是使用ContFont.red这样的方式,而且你可以继续继承,写成ContFont.red.small。 接下来每一个item定义一个属性。定义属性的最好方法就是在api文档里找到这个view的xml属性,比如在EditText中有InputType这个属性,那么在你的style里面你就可以来定义它。
这样一个style就写好了。
使用也非常简单,我们只要在写我们的view时,加入style标签就可以了,就像这样
<TextView
    style="@style/CodeFont"

    android:text="@string/hello" />

下面讲讲主题,前面已经说了。
主题需要在AndroidManifest.xml中注册。如果你想整个程序都使用这个主题,你可以这样写
<application android:theme="@style/CustomTheme">
如果你只需要在某个Activity中使用主题,那么只要在Activity标签中写入android:theme=就可以了,android有很多好的默认主题,比如
<activity android:theme="@android:style/Theme.Dialog">
这就会使你的整个Activity变成一个对话框形式,或者,如果你希望背景是透明的,可以这样写
<activity android:theme="@android:style/Theme.Translucent">
同样的我们也可以继承父类theme,写法和style一样,就不赘述了。当然,和style一样,你也可以自己定义一个theme,写个例子

代码

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <style name="CustomTheme">
 <item name="android:windowNoTitle">true</item>
 <item name="windowFrame">@drawable/screen_frame</item>
 <item name="windowBackground">@drawable/screen_background_white</item>
 <item name="panelForegroundColor">#FF000000</item>
 <item name="panelBackgroundColor">#FFFFFFFF</item>
 <item name="panelTextColor">?panelForegroundColor</item>
 <item name="panelTextSize">14</item>
 <item name="menuItemTextColor">?panelTextColor</item>
 <item name="menuItemTextSize">?panelTextSize</item>
 </style>
</resources>

如果你要在java代码中加载主题的话,只要用setTheme(R.style.CustomTheme)就可以了,不过记得一定要在初始化任何view之前,比如一定要放在我们常用的setContentView()之前。通常,不建议这么做。

android之selector背景选择器

控件常见状态:在XML文件中用到了selector节点,selector可以理解为状态切换器,不同的状态下切换不同的样式,各种状态用Item节点表示,以下为一些常见的状态(注意:statelist中第一个匹配当前状态的item会被使用。因此,如果第一个item没有任何状态特性的话,那么它将每次都被使用,这也是为什么默认的值必须总是在最后,各种状态可以交叉使用):

        1、android:state_pressed
        boolean。“true”表示按下状态使用(例如按钮按下);“false”表示非按下状态使用。

        2、android:state_focused
        boolean。“true”表示聚焦状态使用(例如使用滚动球/d-pad聚焦button);“false”表示非聚焦状态使用。

        3、android:state_selected
        boolean。“true”表示选中状态使用(例如tab打开);“false”表示非选中状态使用。

        4、android:state_checkable
        boolean。“true”表示可勾选状态时使用;“false”表示非可勾选状态使用。(只对能切换可勾选—非可勾选的构件有用。)

        5、android:state_checked
        boolean。“true”表示勾选状态使用;“false”表示非勾选状态使用。

        6、android:state_enabled
        boolean。“true”表示可用状态使用(能接收触摸/点击事件);“false”表示不可用状态使用。

        7、android:window_focused
        boolean。“true”表示应用程序窗口有焦点时使用(应用程序在前台);“false”表示无焦点时使用(例如notification栏拉下或对话框显示)。        
关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法。
首先android的selector是在drawable/xxx.xml中配置的。
先看一下listview中的状态:
把下面的XML文件保存成你自己命名的.xml文件(比如list_item_bg.xml),在系统使用时根据ListView中的列表项的状态来使用相应的背景图片。
drawable/list_item_bg.xml
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 默认时的背景图片-->
    <item android:drawable="@drawable/pic1" />
    <!-- 没有焦点时的背景图片-->
    <item android:state_window_focused="false" android:drawable="@drawable/pic1" />
    <!-- 非触摸模式下获得焦点并单击时的背景图片-->
    <item android:state_focused="true" android:state_pressed="true"
        android:drawable="@drawable/pic2" />
    <!-- 触摸模式下单击时的背景图片-->
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/pic3" />
    <!--选中时的图片背景-->
    <item android:state_selected="true" android:drawable="@drawable/pic4" />
    <!--获得焦点时的图片背景-->
    <item android:state_focused="true" android:drawable="@drawable/pic5" />
</selector>
  使用些xml文件:第一种是在listview中配置android:listSelector="@drawable/list_item_bg"
或者在listview的item中添加属性android:background=“@drawable/list_item_bg"即可实现,或者在java代码中使用:Drawable drawable = getResources().getDrawable(R.drawable.list_item_bg);
       ListView.setSelector(drawable);同样的效果。
但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"
使其透明。
其次再来看看Button的一些背景效果:
android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件
根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。
以下就是配置button中的文字效果:
drawable/button_font.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="#FFF" />
    <item android:state_focused="true" android:color="#FFF" />
    <item android:state_pressed="true" android:color="#FFF" />
    <item android:color="#000" />
</selector>
Button还可以实现更复杂的效果,例如渐变啊等等。
drawable/button_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <!-- 定义当button 处于pressed 状态时的形态。-->
        <shape>
            <gradient android:startColor="#8600ff" />
            <stroke android:width="2dp" android:color="#000000" />
            <corners android:radius="5dp" />
            <padding android:left="10dp" android:top="10dp"
                android:bottom="10dp" android:right="10dp" />
        </shape>
    </item>
    <item android:state_focused="true">
        <!-- 定义当button获得focus时的形态-->
        <shape>
            <gradient android:startColor="#eac100" />
            <stroke android:width="2dp" android:color="#333333" color="#ffffff" />
            <corners android:radius="8dp" />
            <padding android:left="10dp" android:top="10dp"
                android:bottom="10dp" android:right="10dp" />
        </shape>
    </item>
</selector>
最后,需要在包含button的xml文件里添加两项。假如是main.xml 文件,我们需要在<Button />里加两项
android:focusable="true"     
android:backgroud="@drawable/button_color"
这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景,配合应用的整体布局非常之有用啊.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值