From 153e43fbe4a45d72a4b4afba681e08ccb73f8e6a Mon Sep 17 00:00:00 2001 From: yollpoll <403716879@qq.com> Date: Thu, 17 Nov 2022 18:41:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9A=AE=E8=82=A4=E4=B8=BB=E9=A2=98=E5=88=87?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/yollpoll/nmb/App.kt | 26 +++++--- .../main/java/com/yollpoll/nmb/Constant.kt | 18 ----- .../model/repository/LauncherRepository.kt | 2 +- .../nmb/view/activity/HomeActivity.kt | 17 ++++- .../nmb/view/activity/SettingActivity.kt | 60 +++++++++++++---- app/src/main/res/layout/activity_setting.xml | 35 ++++++++-- .../res/layout/item_for_custom_spinner.xml | 11 ++++ app/src/main/res/layout/item_thread.xml | 34 +++++++--- app/src/main/res/values/strings.xml | 1 + .../main/java/com/yollpoll/base/Constant.kt | 21 +++++- .../java/com/yollpoll/base/NMBActivity.kt | 30 +++++++++ .../java/com/yollpoll/base/NmbBaseAdapter.kt | 7 +- base/src/main/java/com/yollpoll/skin/Skin.kt | 9 +-- .../java/com/yollpoll/skin/SkinHandler.kt | 53 ++++++--------- .../com/yollpoll/skin/SkinInflaterFactory.kt | 2 +- .../src/main/java/com/yollpoll/utils/Utils.kt | 66 ++++++++++++++++++- .../res/animator/material_item_animator.xml | 42 ++++++++++++ .../drawable-night/shape_material_item.xml | 9 +++ .../main/res/drawable/shape_material_item.xml | 9 +++ 19 files changed, 350 insertions(+), 102 deletions(-) delete mode 100644 app/src/main/java/com/yollpoll/nmb/Constant.kt create mode 100644 app/src/main/res/layout/item_for_custom_spinner.xml create mode 100644 base/src/main/res/animator/material_item_animator.xml create mode 100644 base/src/main/res/drawable-night/shape_material_item.xml create mode 100644 base/src/main/res/drawable/shape_material_item.xml diff --git a/app/src/main/java/com/yollpoll/nmb/App.kt b/app/src/main/java/com/yollpoll/nmb/App.kt index a279769..35faaa5 100644 --- a/app/src/main/java/com/yollpoll/nmb/App.kt +++ b/app/src/main/java/com/yollpoll/nmb/App.kt @@ -7,9 +7,13 @@ import androidx.annotation.ChecksSdkIntAtLeast import androidx.datastore.core.DataStore import com.google.android.material.color.DynamicColors import com.google.android.material.color.DynamicColorsOptions +import com.yollpoll.arch.message.MessageManager import com.yollpoll.base.NMBApplication import com.yollpoll.base.R +import com.yollpoll.floweventbus.FlowEventBus import com.yollpoll.framework.extensions.getString +import com.yollpoll.framework.extensions.putString +import com.yollpoll.framework.extensions.saveBean import com.yollpoll.nmb.db.MainDB import com.yollpoll.nmb.model.bean.CookieBean import com.yollpoll.skin.SkinTheme @@ -59,17 +63,21 @@ class App : NMBApplication() { return true } - /** - * 获取attr中的颜色 - */ - fun getAttrColor(id: Int): Int { - return this.getAttrColor(id) - } + var appSkinTheme: SkinTheme = SkinTheme.NULL + set(value) { + field = value + skinTheme=value + GlobalScope.launch { + putString("theme", value.name) + } + } - fun initTheme() { + private fun initTheme() { GlobalScope.launch { - val theme = getString("theme", SkinTheme.MATERIAL.name) - skinTheme = SkinTheme.valueOf(theme) + appSkinTheme = SkinTheme.valueOf(getString("theme", SkinTheme.NULL.name)) + skinTheme=appSkinTheme } } + + } \ No newline at end of file diff --git a/app/src/main/java/com/yollpoll/nmb/Constant.kt b/app/src/main/java/com/yollpoll/nmb/Constant.kt deleted file mode 100644 index 1beea46..0000000 --- a/app/src/main/java/com/yollpoll/nmb/Constant.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.yollpoll.nmb - -const val KEY_FORUM_LIST = "forum_list" -const val USER_AGENT="NMBClient-yollpoll-android" -const val APP_ID="yollpoll" -const val KEY_CUR_COOKIE="current_cookie" -const val KEY_SHOW_ANNOUNCEMENT="show_announcement" -const val KEY_OPEN_CRASH_HANDLER="open_crash_handler" - -//eventbus msg -const val ACTION_TAG_NAME="action_tag_name" -const val ACTION_TAG_ID="action_tag_id" - -const val AUTHOR="yollpoll" -const val GIT="https://github.com/yollpoll/nmb" -const val EMAIL="iamspq@gmail.com" -const val FIR="http://d.firim.pro/newnmb"//fir仓库 - diff --git a/app/src/main/java/com/yollpoll/nmb/model/repository/LauncherRepository.kt b/app/src/main/java/com/yollpoll/nmb/model/repository/LauncherRepository.kt index 3bdb749..bad6861 100644 --- a/app/src/main/java/com/yollpoll/nmb/model/repository/LauncherRepository.kt +++ b/app/src/main/java/com/yollpoll/nmb/model/repository/LauncherRepository.kt @@ -1,8 +1,8 @@ package com.yollpoll.nmb.model.repository import android.util.Log -import com.yollpoll.base.TAG import com.yollpoll.framework.net.http.RetrofitFactory +import com.yollpoll.nmb.TAG import com.yollpoll.nmb.di.LauncherRetrofitFactory import com.yollpoll.nmb.net.DIRECT_BASE_URL import com.yollpoll.nmb.net.HttpService diff --git a/app/src/main/java/com/yollpoll/nmb/view/activity/HomeActivity.kt b/app/src/main/java/com/yollpoll/nmb/view/activity/HomeActivity.kt index 8d76608..dacb75c 100644 --- a/app/src/main/java/com/yollpoll/nmb/view/activity/HomeActivity.kt +++ b/app/src/main/java/com/yollpoll/nmb/view/activity/HomeActivity.kt @@ -31,6 +31,9 @@ import com.yollpoll.annotation.annotation.Route import com.yollpoll.arch.annotation.PermissionAuto import com.yollpoll.arch.annotation.ViewModel import com.yollpoll.arch.log.LogUtils +import com.yollpoll.arch.message.MessageManager +import com.yollpoll.arch.message.liveeventbus.LiveEventBus +import com.yollpoll.arch.message.liveeventbus.ObserverWrapper import com.yollpoll.base.* import com.yollpoll.framework.dispatch.DispatchRequest import com.yollpoll.framework.dispatch.OnBackListener @@ -55,6 +58,7 @@ import com.yollpoll.nmb.net.realCover import com.yollpoll.nmb.router.DispatchClient import com.yollpoll.nmb.router.ROUTE_HOME import com.yollpoll.nmb.view.widgets.* +import com.yollpoll.skin.SkinTheme import com.yollpoll.utils.copyStr import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.lifecycle.HiltViewModel @@ -258,6 +262,14 @@ class HomeActivity : NMBActivity() { } } } + //切换主题刷新界面 + override fun onThemeChanged(theme: SkinTheme) { + super.onThemeChanged(theme) + mDataBinding.refresh.isRefreshing=true + mDataBinding.rvContent.adapter=adapterThread + refreshThread() + } + /** * 新建串 @@ -298,8 +310,9 @@ class HomeActivity : NMBActivity() { ImageActivity.gotoImageActivity(context, 0, arrayListOf("封面"), arrayListOf(realCover)) } } - fun gotoVisitHistory(){ - lifecycleScope.launch{ + + fun gotoVisitHistory() { + lifecycleScope.launch { gotoHistory(context) } } diff --git a/app/src/main/java/com/yollpoll/nmb/view/activity/SettingActivity.kt b/app/src/main/java/com/yollpoll/nmb/view/activity/SettingActivity.kt index 73fb0c5..928e587 100644 --- a/app/src/main/java/com/yollpoll/nmb/view/activity/SettingActivity.kt +++ b/app/src/main/java/com/yollpoll/nmb/view/activity/SettingActivity.kt @@ -1,30 +1,25 @@ package com.yollpoll.nmb.view.activity -import android.Manifest import android.app.Application import android.content.Context -import android.content.Intent import android.os.Bundle +import android.view.View +import android.widget.AdapterView +import android.widget.ArrayAdapter import androidx.activity.viewModels import androidx.databinding.Bindable -import androidx.lifecycle.lifecycleScope import androidx.lifecycle.viewModelScope -import com.permissionx.guolindev.PermissionX import com.yollpoll.annotation.annotation.Route +import com.yollpoll.arch.message.MessageManager import com.yollpoll.base.NMBActivity import com.yollpoll.framework.dispatch.DispatchRequest import com.yollpoll.framework.extensions.* -import com.yollpoll.framework.fast.FastActivity import com.yollpoll.framework.fast.FastViewModel -import com.yollpoll.framework.utils.getBoolean -import com.yollpoll.nmb.BR -import com.yollpoll.nmb.KEY_OPEN_CRASH_HANDLER -import com.yollpoll.nmb.MyCrashHandler -import com.yollpoll.nmb.R +import com.yollpoll.nmb.* import com.yollpoll.nmb.databinding.ActivitySettingBinding import com.yollpoll.nmb.router.DispatchClient import com.yollpoll.nmb.router.ROUTE_SETTING -import com.yollpoll.utils.saveBitmapTpMediaStore +import com.yollpoll.skin.SkinTheme import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch @@ -43,18 +38,59 @@ class SettingActivity : NMBActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + initView() + } + + private fun initView() { initTitle(mDataBinding.headerTitle.toolbar, true) mDataBinding.switchLog.setOnCheckedChangeListener { view, checked -> vm.openCrashHandler(checked) } + //uiTheme + var mThemeAdapter = ArrayAdapter( + this, + R.layout.item_for_custom_spinner, + SkinTheme.values().map { + return@map when (it) { + SkinTheme.NULL -> "无" + SkinTheme.MATERIAL -> "Material Design" + SkinTheme.OLD_SCHOOL -> "复古怀旧" + SkinTheme.OTHER -> "其他" + } + } + ) + mDataBinding.spinner.adapter = mThemeAdapter + mDataBinding.spinner.setSelection(SkinTheme.values().indexOf(App.INSTANCE.appSkinTheme)) + mDataBinding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { + override fun onItemSelected( + parent: AdapterView<*>?, + view: View?, + position: Int, + id: Long + ) { + MessageManager.getInstance().sendMessage(ACTION_SELECT_THEME,SkinTheme.values()[position]) + App.INSTANCE.appSkinTheme = SkinTheme.values()[position] + } + + override fun onNothingSelected(parent: AdapterView<*>?) { + + } + + } + } + + fun selectTheme(view: View) { + } } @HiltViewModel class SettingVm @Inject constructor(val app: Application, val crashHandler: MyCrashHandler) : FastViewModel(app) { + @Bindable var openLog: Boolean = false + init { initLog() } @@ -68,7 +104,7 @@ class SettingVm @Inject constructor(val app: Application, val crashHandler: MyCr fun openCrashHandler(open: Boolean) { viewModelScope.launch { - openLog=open + openLog = open app.putBoolean(KEY_OPEN_CRASH_HANDLER, open) } } diff --git a/app/src/main/res/layout/activity_setting.xml b/app/src/main/res/layout/activity_setting.xml index 6cb2b8a..6246ed5 100644 --- a/app/src/main/res/layout/activity_setting.xml +++ b/app/src/main/res/layout/activity_setting.xml @@ -16,7 +16,7 @@ + android:layout_height="@dimen/item_height" + android:visibility="gone"> + + + + + + + + + diff --git a/app/src/main/res/layout/item_for_custom_spinner.xml b/app/src/main/res/layout/item_for_custom_spinner.xml new file mode 100644 index 0000000..ce4d1ee --- /dev/null +++ b/app/src/main/res/layout/item_for_custom_spinner.xml @@ -0,0 +1,11 @@ + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_thread.xml b/app/src/main/res/layout/item_thread.xml index d193a54..4a573ba 100644 --- a/app/src/main/res/layout/item_thread.xml +++ b/app/src/main/res/layout/item_thread.xml @@ -10,11 +10,22 @@ + + + - + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bd38881..6411c8e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -52,4 +52,5 @@ 检测新版本 导入 >>No. + UI风格 \ No newline at end of file diff --git a/base/src/main/java/com/yollpoll/base/Constant.kt b/base/src/main/java/com/yollpoll/base/Constant.kt index 77582b0..447cb1d 100644 --- a/base/src/main/java/com/yollpoll/base/Constant.kt +++ b/base/src/main/java/com/yollpoll/base/Constant.kt @@ -1,3 +1,20 @@ -package com.yollpoll.base +package com.yollpoll.nmb + +const val TAG = "nmb" +const val KEY_FORUM_LIST = "forum_list" +const val USER_AGENT = "NMBClient-yollpoll-android" +const val APP_ID = "yollpoll" +const val KEY_CUR_COOKIE = "current_cookie" +const val KEY_SHOW_ANNOUNCEMENT = "show_announcement" +const val KEY_OPEN_CRASH_HANDLER = "open_crash_handler" + +//eventbus msg +const val ACTION_TAG_NAME = "action_tag_name" +const val ACTION_TAG_ID = "action_tag_id" +const val ACTION_SELECT_THEME = "action_select_theme" + +const val AUTHOR = "yollpoll" +const val GIT = "https://github.com/yollpoll/nmb" +const val EMAIL = "iamspq@gmail.com" +const val FIR = "http://d.firim.pro/newnmb"//fir仓库 - const val TAG = "nmb" \ No newline at end of file diff --git a/base/src/main/java/com/yollpoll/base/NMBActivity.kt b/base/src/main/java/com/yollpoll/base/NMBActivity.kt index b5358ab..aa4ef00 100644 --- a/base/src/main/java/com/yollpoll/base/NMBActivity.kt +++ b/base/src/main/java/com/yollpoll/base/NMBActivity.kt @@ -6,9 +6,18 @@ import android.view.Menu import android.view.View import androidx.appcompat.app.ActionBar import androidx.appcompat.widget.Toolbar +import androidx.core.view.LayoutInflaterCompat import androidx.databinding.ViewDataBinding +import androidx.lifecycle.asLiveData +import androidx.lifecycle.observe +import com.yollpoll.arch.message.liveeventbus.LiveEventBus +import com.yollpoll.arch.message.liveeventbus.ObserverWrapper +import com.yollpoll.floweventbus.FlowEventBus import com.yollpoll.framework.fast.FastActivity import com.yollpoll.framework.fast.FastViewModel +import com.yollpoll.nmb.ACTION_SELECT_THEME +import com.yollpoll.skin.SkinInflaterFactory +import com.yollpoll.skin.SkinTheme /** * Created by spq on 2022/6/22 @@ -22,6 +31,23 @@ abstract class NMBActivity : FastAct return mViewModel } + override fun onCreate(savedInstanceState: Bundle?) { + LayoutInflaterCompat.setFactory2(layoutInflater,SkinInflaterFactory) + super.onCreate(savedInstanceState) + LiveEventBus.use(ACTION_SELECT_THEME, SkinTheme::class.java) + .observe(this, object : ObserverWrapper() { + override fun isSticky() = true + override fun onChanged(value: SkinTheme?) { + value?.let { + onThemeChanged(value) + } + } + + override fun mainThread() = true + + }) + } + abstract fun initViewModel(): VM @@ -61,4 +87,8 @@ abstract class NMBActivity : FastAct return null } + open fun onThemeChanged(theme: SkinTheme) { + + } + } \ No newline at end of file diff --git a/base/src/main/java/com/yollpoll/base/NmbBaseAdapter.kt b/base/src/main/java/com/yollpoll/base/NmbBaseAdapter.kt index 32e6b8d..6767242 100644 --- a/base/src/main/java/com/yollpoll/base/NmbBaseAdapter.kt +++ b/base/src/main/java/com/yollpoll/base/NmbBaseAdapter.kt @@ -58,11 +58,10 @@ open class NmbPagingDataAdapter( override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder { context = parent.context - val inflater=LayoutInflater.from(context).cloneInContext(context) - inflater.factory2 = SkinInflaterFactory - +// val inflater=LayoutInflater.from(context).cloneInContext(context) +// inflater.factory2 = SkinInflaterFactory val binding = DataBindingUtil.inflate( - inflater, + LayoutInflater.from(context), layoutId, parent, false diff --git a/base/src/main/java/com/yollpoll/skin/Skin.kt b/base/src/main/java/com/yollpoll/skin/Skin.kt index 5740aeb..c24bf3f 100644 --- a/base/src/main/java/com/yollpoll/skin/Skin.kt +++ b/base/src/main/java/com/yollpoll/skin/Skin.kt @@ -6,6 +6,7 @@ import android.content.res.AssetManager import android.content.res.Resources import android.util.AttributeSet import android.view.View +import android.view.ViewGroup /** * Created by spq on 2022/11/15 @@ -19,7 +20,7 @@ enum class SkinTheme(name: String) { NULL("NULL"), MATERIAL("MATERIAL"), OLD_SCHOOL("OLD_SCHOOL"), OTHER("OTHER") } //函数别名 -typealias SkinHandler = (name: String, view: View, attrs: AttributeSet) -> View +typealias SkinHandler = (parent: View?, name: String, view: View, attrs: AttributeSet) -> View val materialHandlers = hashMapOf(MaterialItemLine, MaterialItem) val oldSchoolHandlers = hashMapOf() @@ -51,12 +52,12 @@ fun getSkinResource(context: Context, path: String): Resources { * @param tag String * @param attrs AttributeSet */ -fun View.setSkin(name: String, tag: String, attrs: AttributeSet):View { +fun View.setSkin(parent: View?, name: String, tag: String, attrs: AttributeSet): View { if (skinTheme == SkinTheme.NULL) return this return handlerPool[skinTheme]?.let { - it[tag]?.invoke(name, this, attrs) - }?:let { + it[tag]?.invoke(parent, name, this, attrs) + } ?: let { it } } \ No newline at end of file diff --git a/base/src/main/java/com/yollpoll/skin/SkinHandler.kt b/base/src/main/java/com/yollpoll/skin/SkinHandler.kt index f5d946a..cc3b0ef 100644 --- a/base/src/main/java/com/yollpoll/skin/SkinHandler.kt +++ b/base/src/main/java/com/yollpoll/skin/SkinHandler.kt @@ -1,48 +1,37 @@ package com.yollpoll.skin -import android.content.Context -import android.opengl.Visibility -import android.util.AttributeSet +import android.animation.AnimatorInflater +import android.animation.StateListAnimator import android.view.View -import android.view.ViewGroup -import android.widget.LinearLayout -import android.widget.RelativeLayout -import androidx.cardview.widget.CardView -import androidx.core.view.children -import androidx.core.view.marginTop -import androidx.core.view.setMargins +import com.google.gson.annotations.Until import com.yollpoll.base.R import com.yollpoll.base.logI import com.yollpoll.framework.extensions.dp2px +import com.yollpoll.utils.setViewMargin /** * Created by spq on 2022/11/16 */ -val MaterialItemLine = Pair(ITEM_LINE) { name, view, attrs -> +val MaterialItemLine = Pair(ITEM_LINE) { parent, name, view, attrs -> view.visibility = View.GONE view } -val MaterialItem = Pair(ITEM) { name, view, attrs -> - if (view is ViewGroup) { - val cardView = CardView(view.context) - //获取构造方法 - val constructor = - view.javaClass.getConstructor(Context::class.java, AttributeSet::class.java) - constructor.isAccessible = true - val realLayout = constructor.newInstance(view.context, attrs) as ViewGroup - - view.children.forEach { - realLayout.addView(it) - } - realLayout.setBackgroundColor(view.context.resources.getColor(R.color.black)) - cardView.addView(realLayout) - cardView.elevation=view.context.dp2px(20f) - cardView.radius=view.context.dp2px(10f) - view.removeAllViews() - "xxxxxxxxxxx".logI() -// view.addView(cardView) - } - "yyyyyyyyyyyyy".logI() +val MaterialItem = Pair(ITEM) { parent, name, view, attrs -> + val context = view.context + val bg = view.context.resources.getDrawable(R.drawable.shape_material_item, null) + view.background = bg + val animator: StateListAnimator = + AnimatorInflater.loadStateListAnimator(view.context, R.animator.material_item_animator) + view.stateListAnimator = animator +// for (i in 0 until attrs.attributeCount) { +// "spq attr name: ${attrs.getAttributeName(i)} attr value: ${attrs.getAttributeValue(i)}".logI() +// } +// parent?.setPadding( +// context.dp2px(10f).toInt(), +// context.dp2px(5f).toInt(), +// context.dp2px(10f).toInt(), +// context.dp2px(5f).toInt() +// ) view } diff --git a/base/src/main/java/com/yollpoll/skin/SkinInflaterFactory.kt b/base/src/main/java/com/yollpoll/skin/SkinInflaterFactory.kt index 1a1787f..dcaa5a1 100644 --- a/base/src/main/java/com/yollpoll/skin/SkinInflaterFactory.kt +++ b/base/src/main/java/com/yollpoll/skin/SkinInflaterFactory.kt @@ -30,7 +30,7 @@ object SkinInflaterFactory : LayoutInflater.Factory2 { attrs.getAttributeValue(NAMESPACE, SKIN_TAG) ?: return null val view: View? = createView(context, name, attrs).let { - it?.setSkin(name, skinTag, attrs) + it?.setSkin(parent,name, skinTag, attrs) } // view?.setBackgroundColor(context.resources.getColor(R.color.black)) // parseSkinAttr(context, attrs, view) diff --git a/base/src/main/java/com/yollpoll/utils/Utils.kt b/base/src/main/java/com/yollpoll/utils/Utils.kt index c289992..c668bbd 100644 --- a/base/src/main/java/com/yollpoll/utils/Utils.kt +++ b/base/src/main/java/com/yollpoll/utils/Utils.kt @@ -8,7 +8,10 @@ import android.os.Build import android.os.Environment import android.provider.DocumentsContract import android.provider.MediaStore -import androidx.core.content.ContextCompat.getSystemService +import android.util.AttributeSet +import android.view.View +import android.view.ViewGroup +import com.yollpoll.framework.extensions.dp2px import java.io.File import java.io.FileNotFoundException import java.text.SimpleDateFormat @@ -221,10 +224,11 @@ fun getCurrentDate(): String { * @param copyStr * @return */ -fun copyStr(context: Context,copyStr: String): Boolean { +fun copyStr(context: Context, copyStr: String): Boolean { return try { //获取剪贴板管理器 - val cm: ClipboardManager? = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager? + val cm: ClipboardManager? = + context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager? // 创建普通字符型ClipData val mClipData: ClipData = ClipData.newPlainText("Label", copyStr) // 将ClipData内容放到系统剪贴板里。 @@ -233,4 +237,60 @@ fun copyStr(context: Context,copyStr: String): Boolean { } catch (e: Exception) { false } +} + +/** + * 设置某个View的margin + * + * @param view 需要设置的view + * @param isDp 需要设置的数值是否为DP + * @param left 左边距 + * @param right 右边距 + * @param top 上边距 + * @param bottom 下边距 + * @return + */ +fun View.setViewMargin( + left: Float? = null, + right: Float? = null, + top: Float? = null, + bottom: Float? = null, + attr:AttributeSet?=null +): ViewGroup.LayoutParams { + val context = this.context + val params: ViewGroup.LayoutParams? = this.layoutParams + var marginParams: ViewGroup.MarginLayoutParams? = null + //获取view的margin设置参数 + marginParams = params?.let { + return@let if (params is ViewGroup.MarginLayoutParams) { + params + } else { + //不存在时创建一个新的参数 + ViewGroup.MarginLayoutParams(params) + } + } ?: let { + ViewGroup.MarginLayoutParams(context,attr) + } + + var leftMargin = marginParams.leftMargin + var rightMargin = marginParams.rightMargin + var topMargin = marginParams.topMargin + var bottomMargin = marginParams.bottomMargin + + left?.let { + leftMargin = context.dp2px(it).toInt() + } + right?.let { + rightMargin = context.dp2px(it).toInt() + } + top?.let { + topMargin = context.dp2px(it).toInt() + } + bottom?.let { + bottomMargin = context.dp2px(it).toInt() + } + //设置margin + marginParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin) + this.layoutParams = marginParams + return marginParams } \ No newline at end of file diff --git a/base/src/main/res/animator/material_item_animator.xml b/base/src/main/res/animator/material_item_animator.xml new file mode 100644 index 0000000..b81dee3 --- /dev/null +++ b/base/src/main/res/animator/material_item_animator.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/base/src/main/res/drawable-night/shape_material_item.xml b/base/src/main/res/drawable-night/shape_material_item.xml new file mode 100644 index 0000000..87a1ea3 --- /dev/null +++ b/base/src/main/res/drawable-night/shape_material_item.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/base/src/main/res/drawable/shape_material_item.xml b/base/src/main/res/drawable/shape_material_item.xml new file mode 100644 index 0000000..b80e963 --- /dev/null +++ b/base/src/main/res/drawable/shape_material_item.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file