Repository: long8313002/SuperXml Branch: master Commit: 010f20792c11 Files: 65 Total size: 78.3 KB Directory structure: gitextract_c62faek5/ ├── .gitignore ├── .idea/ │ ├── codeStyles/ │ │ ├── Project.xml │ │ └── codeStyleConfig.xml │ ├── gradle.xml │ ├── misc.xml │ ├── runConfigurations.xml │ └── vcs.xml ├── README.md ├── app/ │ ├── .gitignore │ ├── build.gradle │ ├── proguard-rules.pro │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── com/ │ │ └── zhangzheng/ │ │ └── superxml/ │ │ └── demo/ │ │ └── ExampleInstrumentedTest.kt │ ├── main/ │ │ ├── AndroidManifest.xml │ │ ├── java/ │ │ │ └── com/ │ │ │ └── zhangzheng/ │ │ │ └── superxml/ │ │ │ └── demo/ │ │ │ ├── MApplication.kt │ │ │ └── MainActivity.kt │ │ └── res/ │ │ ├── drawable/ │ │ │ └── ic_launcher_background.xml │ │ ├── drawable-v24/ │ │ │ └── ic_launcher_foreground.xml │ │ ├── layout/ │ │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26/ │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ └── values/ │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test/ │ └── java/ │ └── com/ │ └── zhangzheng/ │ └── superxml/ │ └── demo/ │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle/ │ └── wrapper/ │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew ├── gradlew.bat ├── library/ │ ├── .gitignore │ ├── build.gradle │ ├── consumer-rules.pro │ ├── proguard-rules.pro │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── com/ │ │ └── zhangzheng/ │ │ └── superxml/ │ │ └── library/ │ │ └── ExampleInstrumentedTest.kt │ ├── main/ │ │ ├── AndroidManifest.xml │ │ ├── java/ │ │ │ └── com/ │ │ │ └── zhangzheng/ │ │ │ └── superxml/ │ │ │ └── library/ │ │ │ ├── LayoutInflateFactoryProxy.kt │ │ │ ├── SuperXml.kt │ │ │ ├── ViewDecorateManager.kt │ │ │ ├── decorate/ │ │ │ │ ├── BackgroundEnableDecorate.kt │ │ │ │ ├── BackgroundPressedDecorate.kt │ │ │ │ ├── BackgroundSelectedDecorate.kt │ │ │ │ ├── BorderDecorate.kt │ │ │ │ ├── IDecorateView.kt │ │ │ │ ├── RadiusDecorate.kt │ │ │ │ ├── SrcRadiusDecorate.kt │ │ │ │ ├── TextColorEnableDecorate.kt │ │ │ │ ├── TextColorPresenterDecorate.kt │ │ │ │ ├── TextColorSelectedDecorate.kt │ │ │ │ └── wrap/ │ │ │ │ ├── CoverChildrenWrapDecorate.kt │ │ │ │ ├── DottedLineWrapDecorate.kt │ │ │ │ ├── IWrapDecorateView.kt │ │ │ │ ├── ScrollWrapDecorate.kt │ │ │ │ └── coverchildren/ │ │ │ │ ├── AbsChildViewParse.kt │ │ │ │ ├── CoverChildrenLayout.kt │ │ │ │ ├── ImageViewCoverParse.kt │ │ │ │ └── TextViewCoverParse.kt │ │ │ ├── ext/ │ │ │ │ ├── ViewBorderExt.kt │ │ │ │ └── ViewRadiusExt.kt │ │ │ └── view/ │ │ │ └── DottedLineView.kt │ │ └── res/ │ │ └── values/ │ │ ├── ids.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test/ │ └── java/ │ └── com/ │ └── zhangzheng/ │ └── superxml/ │ └── library/ │ └── ExampleUnitTest.kt └── settings.gradle ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ *.iml .gradle /local.properties /.idea/caches /.idea/libraries /.idea/modules.xml /.idea/workspace.xml /.idea/navEditor.xml /.idea/assetWizardSettings.xml .DS_Store /build /captures .externalNativeBuild .cxx ================================================ FILE: .idea/codeStyles/Project.xml ================================================
xmlns:android ^$
xmlns:.* ^$ BY_NAME
.*:id http://schemas.android.com/apk/res/android
.*:name http://schemas.android.com/apk/res/android
name ^$
style ^$
.* ^$ BY_NAME
.* http://schemas.android.com/apk/res/android ANDROID_ATTRIBUTE_ORDER
.* .* BY_NAME
================================================ FILE: .idea/codeStyles/codeStyleConfig.xml ================================================ ================================================ FILE: .idea/gradle.xml ================================================ ================================================ FILE: .idea/misc.xml ================================================ ================================================ FILE: .idea/runConfigurations.xml ================================================ ================================================ FILE: .idea/vcs.xml ================================================ ================================================ FILE: README.md ================================================ # android布局能力增加,轻松实现圆角、边框、虚线、属性覆盖等功能。完全无侵入 有排版: https://blog.csdn.net/long8313002/article/details/108703057 概述 android开发中,我们常常使用xml来写布局文件,这种实现方式不仅简单,而且表达能力更强。但是google提供的布局属性有限,有些功能的实现我们不得不使用代码,或者自定义控件的方式来实现。那有没有一种方法,可以将属性增强来实现额外的功能呢?例如我们常常使用background 来表示和设置背景,那是不是可以使用layout_radius来表示和设置圆角呢? 使用示例 需要在项目build.gradle中引用依赖 implementation 'com.zhangzheng.superxml:library:1.1.0' 另外在Application注册一行代码 SuperXml.init(this) OVER 能力说明 属性增强 圆角: 说明 app:layout_radius 支持将控件背景设置为圆角,背景支持纯色背景或者图片,另外对于ImageView 的src如果想设置成圆角需要使用app:layout_src_radius,例如 复合属性 说明 一般情况下,我们要表示点击和普通状态下不同的字体颜色,或者背景会使用selector来定义一个文件,然后在布局文件中引用,一方面这样的使用很麻烦,另外一方面可读性也会降低(用户需要进入selector文件进行分析,才知道代码表达意图)。这边封装了常用的复用属性,如下: 属性 属性类型 说明 layout_background_enableTrue layout_background_enableFalse reference|color(资源或者颜色) 背景(是否可用) layout_background_pressedTrue layout_background_pressedFalse reference|color(资源或者颜色) 背景(是否按压) layout_background_selectedTrue layout_background_selectedTrue reference|color(资源或者颜色) 背景(是否选择) layout_textColor_enableTrue layout_textColor_enableFalse reference|color(资源或者颜色) 字体颜色(是否可用) layout_textColor_pressedTrue layout_textColor_pressedFalse reference|color(资源或者颜色) 字体颜色(是否按压) layout_textColor_selectedTrue layout_textColor_selectedFalse reference|color(资源或者颜色) 字体颜色(是否选择) 边框 说明 比较简单,layout_border_color表示边框颜色,layout_border_width表示边框粗细,和radius一起使用表示边框圆角。 虚线 说明 可以在任何视图上使用(建议在View中定义),必须同时定义grap、dash_height、dash_width。支持横虚线,和竖虚线,这里会检测视图宽高来确定。属性说明:dash_grap(虚线间距)、dash_width(单个小线的宽)、dash_height(单个小线的高) 视图替换或增强 这个能力可能会将布局文件中的视图替换成其他控件、或者对其进行增强。 滚动视图 为了适配小屏手机,我们可能会在每一个布局文件中加上一层ScrollView,现在对容器控件进行能力增强。 说明 在需要滚动的视图上添加属性app:layout_canScroll="true",来使其获得滚动的能力。另外所有scrollView的属性,可以配置在该容器控件中。 属性覆盖 有一种很常见的业务场景,在一个条目中有多个控件,控件大多数属性是相同的(例如TextView的字体颜色、大小等),一般我们会给每一个控件加上相同的属性(冗余)、或者定义公共样式(太麻烦)。现在参考html的布局方式,在父控件中设置公共样式,给子控件当默认值。 支持的属性 控件类型 属性 TextView textColor textSize text maxLines maxEms textColorHint hint textDirection textStyle capitalize ImageView src scaleType 扩展 属性扩展 SuperXml.addDecorate(object : IDecorateView() { override fun initExtraInfo(typedArray: TypedArray): Boolean { } override fun decorate(view: View) { } }) 实现参考 internal class RadiusDecorate(var radius: Float = 0f) : IDecorateView() { override fun initExtraInfo(typedArray: TypedArray): Boolean { radius = typedArray.getDimension(R.styleable.decorate_view_layout_radius,0f) return radius > 0 } override fun decorate(view: View)= view.setRadius(radius) } 控件替换 OR 增强 SuperXml.addDecorate(object :IWrapDecorateView(){ override fun decorateView(view: View): View { } override fun initExtraInfo(typedArray: TypedArray): Boolean { } }) 实现参考 internal class ScrollWrapDecorate(var canScroll: Boolean = false) : IWrapDecorateView() { override fun initExtraInfo(typedArray: TypedArray): Boolean { canScroll = typedArray.getBoolean(R.styleable.decorate_view_layout_canScroll, false) return canScroll } override fun decorateView(view: View): View { return ScrollViewProxy( view, attributeSet ) } } 属性覆盖 SuperXml.addCoverAttributeParse(object : AbsChildViewParse(){ override fun createInfoView(context: Context, attributeSet: AttributeSet?): TextView { } override fun coverAttribute(): MutableList<*> { } }) 实现参考 class TextViewCoverParse : AbsChildViewParse() { override fun createInfoView(context: Context, attributeSet: AttributeSet?): TextView = TextView(context, attributeSet) override fun coverAttribute(): MutableList<*> = mutableListOf( AttributeInfo("textSize",{ textSize }) { value -> textSize = value }, AttributeInfo("textColor",{ textColors }) { value -> setTextColor(value) }, AttributeInfo("text",{ text }) { text -> setText(text) }, AttributeInfo("maxLines",{ maxLines }) { maxLines -> setMaxLines(maxLines) }, AttributeInfo("maxEms",{ maxEms }) { maxEms -> setMaxEms(maxEms) }, AttributeInfo("textColorHint",{ hintTextColors }) { hintTextColors -> setHintTextColor(hintTextColors) }, AttributeInfo("hint",{ hint }) { hint -> setHint(hint) }, AttributeInfo("textDirection",{ textDirection }) { textDirection -> setTextDirection(textDirection) }, AttributeInfo("textStyle",{ typeface }) { typeface -> setTypeface(typeface) }, AttributeInfo("capitalize",{ inputType }) { inputType -> setInputType(inputType) } ) } 代码:github https://github.com/long8313002/SuperXml ================================================ FILE: app/.gitignore ================================================ /build ================================================ FILE: app/build.gradle ================================================ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 buildToolsVersion "30.0.2" defaultConfig { applicationId "com.zhangzheng.superxml.demo" minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { kotlinOptions.freeCompilerArgs += ['-module-name', "com.zhangzheng.superxml"] } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.core:core-ktx:1.3.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation project(":library") } ================================================ FILE: app/proguard-rules.pro ================================================ # Add project specific ProGuard rules here. # You can control the set of applied configuration files using the # proguardFiles setting in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} # Uncomment this to preserve the line number information for # debugging stack traces. #-keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile ================================================ FILE: app/src/androidTest/java/com/zhangzheng/superxml/demo/ExampleInstrumentedTest.kt ================================================ package com.zhangzheng.superxml.demo import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.Test import org.junit.runner.RunWith import org.junit.Assert.* /** * Instrumented test, which will execute on an Android device. * * See [testing documentation](http://d.android.com/tools/testing). */ @RunWith(AndroidJUnit4::class) class ExampleInstrumentedTest { @Test fun useAppContext() { // Context of the app under test. val appContext = InstrumentationRegistry.getInstrumentation().targetContext assertEquals("com.zhangzheng.superxml.demo", appContext.packageName) } } ================================================ FILE: app/src/main/AndroidManifest.xml ================================================ ================================================ FILE: app/src/main/java/com/zhangzheng/superxml/demo/MApplication.kt ================================================ package com.zhangzheng.superxml.demo import android.app.Application import com.zhangzheng.superxml.library.SuperXml class MApplication : Application() { override fun onCreate() { super.onCreate() SuperXml.init(this) } } ================================================ FILE: app/src/main/java/com/zhangzheng/superxml/demo/MainActivity.kt ================================================ package com.zhangzheng.superxml.demo import android.graphics.Color import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import androidx.appcompat.app.AppCompatActivity import com.zhangzheng.superxml.library.ext.setBorder import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } } ================================================ FILE: app/src/main/res/drawable/ic_launcher_background.xml ================================================ ================================================ FILE: app/src/main/res/drawable-v24/ic_launcher_foreground.xml ================================================ ================================================ FILE: app/src/main/res/layout/activity_main.xml ================================================