GithubHelp home page GithubHelp logo

dropshapexml's Introduction

丢掉你的Shape selector layer_list XML文件

项目里的shape.xml selector.xml layer_list.xml 文件太多啦

想个办法替代吧:

1. shape.xml 的替代法

  • 旧写法 - shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#f00000"/>
    <corners android:radius="15dp"/>
</shape>
  • 新写法-kotlin
textView.background = shape {
    shape = RECTANGLE
    solid { color = Color.parseColor("#f00000") }
    corners { radius = 15f.dp }
}

xml 里所有的属性都支持

2. selector.xml 的替代法

  • 旧写法 - selector.xml

normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#f00000"/>
    <corners android:radius="15dp"/>
</shape>

pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#00f000"/>
    <corners android:radius="15dp"/>
</shape>

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/normal" android:state_pressed="false"/>
</selector>
  • 新写法 - kotlin
textView.background = selector {
    item {
        drawable = shape {
            shape = RECTANGLE
            solid { color = Color.parseColor("#f00000") }
            corners { radius = 15f.dp }
        }
        state_pressed = false
    }
    item {
        drawable = shape {
            shape = RECTANGLE
            solid { color = Color.parseColor("#00f000") }
            corners { radius = 15f.dp }
        }
        state_pressed = true
    }
}

3. layer_list.xml 的替代法

  • 旧写法 - layer_list.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#C15454" />
            <corners android:radius="15dp" />
        </shape>
    </item>
    <item android:top="50dp">
        <shape android:shape="rectangle">
            <solid android:color="#155473" />
            <corners
                android:bottomLeftRadius="15dp"
                android:bottomRightRadius="15dp" />
        </shape>
    </item>
</layer-list>
  • 新写法 - kotlin
layerList {
   item {
       drawable = shape {
           solid { color = Color.parseColor("#C15454") }
           corners { radius = 15f.dp }
       }
   }
   item {
       top = 50.dp
       drawable = shape {
           solid { color = Color.parseColor("#155473") }
           corners {
               leftBottomRadius = 15f.dp
               rightBottomRadius = 15f.dp
           }
       }
   }
}

4. selector_color.xml 颜色选择的替代法

  • 旧写法 - selector_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/green" android:state_pressed="true"/>
    <item android:drawable="@color/white" android:state_pressed="false"/>
</selector>
  • 新写法 - kotlin
textView.setTextColor(colorSelector {
     item {
         state_pressed = true
         color = Color.WHITE
     }
     item {
         state_pressed = false
         color = Color.GREEN
     }
 })

5. bitmap.xml 图片的替代法

  • 旧写法 - bitmap.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:tint="@color/white"
    android:src="@drawable/icon_setting"/>
  • 新写法 - kotlin
bitmap {
    src = R.drawable.icon_setting
    tint = Color.WHITE
}

dropshapexml's People

Contributors

xiaolei123 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.