zoukankan      html  css  js  c++  java
  • android中样式和自定义button样式

    1)自定义button样式

    一、采用图片方式 

        首先新建Android XML文件,类型选Drawable,根结点选selector,自定义一个文件名。

          随后,开发环境自动在新建的文件里加了selector结点,我们只需要在selector结点里写上三种状态时显示的背景图片(按下、获取焦点,正常)即可。具体如下:

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

         注:这里获取焦点跟点击时显示的是同一张图片,必须严格照上面的顺序写,不可倒。
      最后,只要在布局时写Button控件时应用到Button的Background属性即可,如:

    <Button android:id="@+id/button1"
     android:layout_width="wrap_content" 
    android:layout_height
    ="wrap_content" android:background="@drawable/button_style"> </Button>

    二、采用自定义方式

    在源代码中,只需要修改button_style文件,同样三种状态分开定义:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true">
            <shape>
                <gradient android:startColor="#0d76e1" 
    android:endColor
    ="#0d76e1" android:angle="270" /> <stroke android:width="1dip" android:color="#f403c9" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item android:state_focused="true"> <shape> <gradient android:startColor="#ffc2b7" android:endColor="#ffc2b7" android:angle="270" /> <stroke android:width="1dip" android:color="#f403c9" /> <corners android:radius="2dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> <item> <shape> <gradient android:startColor="#000000" android:endColor="#ffffff" android:angle="180" /> <stroke android:width="1dip" android:color="#f403c9" /> <corners android:radius="5dip" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item> </selector>

    注:代码中的各属性含义为:

        gradient 主体渐变

        startColor开始颜色,endColor结束颜色 ,

        angle开始渐变的角度(值只能为90的倍数,0时为左到右渐变,90时为下到上渐变,依次逆时针类推)
        stroke 边框 width 边框宽度,color 边框颜色
        corners 圆角 radius 半径,0为直角
        padding text值的相对位置

    2)自定义style样式

    一、在style.xml中自定义样式

     以自定义text文本大小和颜色为例,自定义一个名称为"testStyle"的style代码如下:

    <resources xmlns:android="http://schemas.android.com/apk/res/android"> 
        <style name="AppBaseTheme" parent="android:Theme.Light"> 
        </style> 
        <style name="AppTheme" parent="AppBaseTheme"> 
        </style> 
    
         <style name="testStyle">  
            <item name="android:textSize">30px</item>  
            <item name="android:textColor">#1110CC</item> 
            <item name="android:width">150dip</item> 
            <item name="android:height">150dip</item> 
        </style> 
    </resources>  

    二、在layout文件中引用自定义的"testStyle"的style样式

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        tools:context=".MainActivity" > 
    
        <TextView 
            style="@style/testStyle" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:gravity="center" 
            android:layout_centerHorizontal="true" 
            android:layout_centerVertical="true" 
            android:text="@string/hello_world" /> 
    
    </RelativeLayout>  

    从以上代码可以看出,应用方式为@style/testStyle

  • 相关阅读:
    2017-2018-1 20155326 实验四 外设驱动程序设计
    2017-2018-1 20155326 20155320《信息安全技术》实验四 木马及远程控制技术
    20155326 2017-2018-1 《信息安全系统设计基础》第六章课上考试题
    6月20日云栖精选夜读:阿里怎么发工资?自研薪酬管理系统首次曝光
    一个成功的研发团队应具备的9大属性
    那些创业的艰辛整理
    明明可以靠脸吃饭偏要靠才华_你身边有女神程序员吗?
    程序猿们_一二三四线城市你更愿意选择去哪里工作?
    微服务架构实践之邮件通知系统改造
    谈谈“僵尸猎手小明”手游兼容性踩到的坑
  • 原文地址:https://www.cnblogs.com/snow-flower/p/6082380.html
Copyright © 2011-2022 走看看