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

简单button样式的设置(不是直接将背景设置为图片,而是设置背景的颜色)

2016-10-22 22:51 344 查看
在以前,我们直接就是在button的brackgroundd中编写drawable,button的点击图片,但是现在我们统一通过style来实现样式的设置,这样设置,更加规范化

步骤:

1.首先编写button的代码:

<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登 录"
android:layout_marginTop="30dp"
android:layout_margin="20dp"
style="@style/bigRedButton"
/>


2.看上面只有    style="@style/bigRedButton"   ,这个样式没有定义,所以我们需要定义这个样式

在stytle中新建bigredButton的样式

<!--button的样式-->
<style name="bigRedButton" >
<item name="android:background">@drawable/bg_btn_style_red</item>
</style>


3.我们看到上面bg_btn_style_red没有,所以要定义:

<?xml version="1.0" encoding="utf-8"?>

<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape android:shape="rectangle">
<corners android:radius="@dimen/buttonCornerSize" />
<solid android:color="@color/red_btn_color_disable" />
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="@dimen/buttonCornerSize" />
<solid android:color="@color/red_btn_color_pressed" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="@dimen/buttonCornerSize" />
<solid android:color="@color/red_btn_color_normal" />
</shape>
</item>
</selector>


4.再看上面corners android:radius="@dimen/buttonCornerSize" />没有定义,我们需要在values中新建dimens的文件,然后在里面定义样式

<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="buttonCornerSize">2.0dip</dimen>
</resources>


最后就是我们的效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐