android之UI美化

先说drawable,drawable除了可以放图片之外还可以放自己编辑的样式。例如以下代码,这里我们在drawable文件下新建个drawable resource file,选择shape类型:

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<!--这里是shape,有矩形,椭圆,线性,菱形,我们选择矩形 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <!-- 这里表示边角圆滑的大小-->
    <corners android:radius="20dp"/>
    <!--这里表示颜色渐变效果 -->
    <gradient
        android:angle="45"
        android:centerColor="@color/blue"
        android:endColor="@color/blue"
        android:startColor="@color/blue"/>
    <!-- 这里是填充,不多说-->
    <padding
        android:bottom="7dp"
        android:left="7dp"
        android:right="7dp"
        android:top="7dp"/>
</shape>

 这个弄好之后我们就可以在UI定义的时候来设置background选择了。 

但我们有时候需要默认一种风格,按压的时候再来一种风格,这样我们就需要用到selector。

老样子,还是在drawable文件下新建个drawable resource file,选择selector类型:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/shape"
        android:state_pressed="false"/>
    <item
        android:drawable="@drawable/button"
        android:state_pressed="true"/>
</selector>
这样设置好之后我们就可以在background里引用这个资源了。忘了说,需要两个shape的drawable来提供,还有我发现start color,end color,centercolor要全选一样,要不然没有点击效果?为什么?

这样全搞完之后我们还需要一个style来节省我们的操作。

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>
    <!-- AppTheme.wrap表示继承AppTheme类型的style-->
    <style name="AppTheme.wrap">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@drawable/selector</item>
    </style>
</resources>
这样我们以后定义控件的时候就可以直接style了,不用每个控件都定义属性什么的,面向对象嘛,是吧!

最后我们这里来讲个Theme。意思就是风格吧。我们可以在manifest的application里设置一种自己定义的样式,也可以设置成其他的android自带的theme。包括设置成dialog形式哦!很好玩哦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值