您的位置:首页 > Web前端 > CSS

使用样式消除重复代码

2017-01-12 23:28 302 查看
1.尝试更改按钮的背景颜色(values/styles.xml)

<style name="RemoteButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:textColor">#556699</item>
<item name="android:textSize">20dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:background">#ccd7ee</item>
</style>


Dialog样式

<style name="MyDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<!-- 边框 -->
<item name="android:windowNoTitle">true</item>
<!-- 无标题 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsTranslucent">false</item>
<!-- 半透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 背景透明 #00000000 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 模糊 -->
</style>


2.创建一个默认的res/drawable目录。然后在该目录下,以shape为根元素创建一个名为button_shape_normal.xml的文件

该XML文件定义了一个圆角矩形。corner元素指定了圆角矩形的圆角半径,而gradient元素则指定了色彩渐变的方向以及起始颜色。 也可使用shape创建其他各种图形,如椭圆、线条以及环等

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<gradient android:angle="90"
android:endColor="#cccccc"
android:startColor="#acacac" />
</shape>


3.在styles.xml中,更新按钮的样式定义,使用新建的Drawable作为按钮的背景

<style name="RemoteButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:textColor">#556699</item>
<item name="android:textSize">20dp</item>
<item name="android:layout_margin">3dp</item>
<!--  <item name="android:background">#ccd7ee</item> -->
<item name="android:background">@drawable/button_shape_normal</item>
</style>


4. state list drawable

使用state list drawable,可根据关联View的不同状态显示不同的drawable。

复制一份button_shape_normal.xml文件,并命名为button_shape_pressed.xml。然后打开该XML文件,将其中定义的角度属性值增加180度,以改变渐变方向,

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<gradient android:angle="90"
android:angle="270"
android:endColor="#cccccc"
android:startColor="#acacac" />
</shape>


接下来,我们需要一个state list drawable。state list drawable必须包含一个selector根元素,以及用来描述状态的一个或多个item。右键单击res/drawable/目录,以selector为根元素,创建一个名为button_shape.xml的文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_shape_normal" android:state_pressed="false"/>
<item android:drawable="@drawable/button_shape_pressed" android:state_pressed="true"/>
</selector>


未点击状态下,按钮显示的是深色文字,其与浅色系背景搭配比较合适。由于背景比较灰暗,因此在未点击状态下,按钮浅色系的背景看上去比较合适。而在点击状态下,使用深色系背景比较合适。与state list shape的创建类似,我们也可以轻松创建并使用state list color。 右键单击res/drawable/目录,创建另一个名为button_text_color.xml的state list drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:color="#ffffff"/>
<item android:state_pressed="true" android:color="#556699"/>
</selector>
</selector>


在styles.xml中,调整按钮样式使用新建背景drawable和新建文字颜色

<style name="RemoteButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:textColor">#556699</item>
<item name="android:textSize">20dp</item>
<item name="android:layout_margin">3dp</item>
<!--   <item name="android:background">@drawable/button_shape_normal</item> -->
<!--    <item name="android:background">@drawable/button_shape</item> -->
<item name="android:textColor">@drawable/button_text_color</item>
</style>


5. layer list 与 inset drawable

shape drawable没有可用的阴影属性。但使用其他两种类型的XML drawable,可自创阴影效果。这两种XML drawable 类型分别是:layer list drawable和inset drawable。

下面介绍创建阴影效果的方法。首先,使用与当前按钮drawable同样的shape创建一个阴影。然后,使用layer-list将阴影shape与当前按钮组合起来,再使用inset对按钮底边进行适当的短距移位,直到能够看到阴影显示。 在res/drawable/目录下,以layer-list为根元素,创建一个名为button_shape_shadowed.xml文件

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<gradient
android:angle="90"
android:centerColor="#303339"
android:centerY="0.05"
android:endColor="#000000"
android:startColor="#00000000" />
</shape>
<inset
android:drawable="@drawable/button_shape"
android:insetBottom="5dp" />
</item>
</layer-list>


可以看到,layer-list元素包含了多个Drawable,并以从后至前的绘制顺序进行排序。列表中第二个drawable是一个inset drawable,其任务就是在已创建的drawable底部做5dp单位的移位,并刚好落在移位形成的阴影上。 注意,阴影drawable并未使用单独的文件,而是直接被嵌入了layer list中。该技巧同样适用于其他drawable,如前面讲到的state list drawable。可自行决定究竟是嵌套drawable还是将其放入单独的文件使用。以单独文件的形式使用drawable可减少重复代码,简化各相关文件,但这也同时会使drawable/目录充斥着大量文件。不过要记住,应总是以简单且易于理解的方式编写代码。

在styles.xml中,修改按钮的样式定义,指向可显示阴影的新建drawable。

<style name="RemoteButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<!-- <item name="android:textColor">#556699</item> -->
<item name="android:textColor">@drawable/button_text_color</item>
<item name="android:textSize">20dp</item>
<item name="android:layout_margin">3dp</item>
<!-- <item name="android:background">#ccd7ee</item> -->
<item name="android:background">@drawable/button_shape_shadowed</item>
</style>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: