新增自定义布局支持,可参考app\src\main\res\layout\demo.xml

修改布局id命名,防止与宿主id冲突
This commit is contained in:
Yutousama 2022-05-26 10:18:36 +08:00
parent c59516aa2a
commit c64e8678a0
5 changed files with 55 additions and 22 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
*.iml *.iml
.gradle .gradle
/local.properties /local.properties
/.idea
/.idea/caches /.idea/caches
/.idea/libraries /.idea/libraries
/.idea/modules.xml /.idea/modules.xml

View File

@ -17,14 +17,15 @@ import com.yutou.popdialog.utils.POPDialogUtils
import com.yutou.popdialog.views.ExitButton import com.yutou.popdialog.views.ExitButton
class POPDialog(context: Activity) : Dialog(context) { class POPDialog : Dialog {
private val activityContext: Activity = context private val activityContext: Activity
private var defLayoutResId = R.layout.pop_dialog_layout
private lateinit var title: TextView private lateinit var title: TextView
private val layout: FrameLayout by lazy { private val layout: ViewGroup by lazy {
findViewById(R.id.title) findViewById(R.id.pop_dialog_title)
} }
private val dialogContext: FrameLayout by lazy { private val dialogContext: ViewGroup by lazy {
findViewById(R.id.context) findViewById(R.id.pop_dialog_content)
} }
private lateinit var view: View private lateinit var view: View
@ -35,7 +36,15 @@ class POPDialog(context: Activity) : Dialog(context) {
private var showModel = 0 private var showModel = 0
private var expansionY = 0f private var expansionY = 0f
init { constructor(context: Activity) : super(context) {
activityContext = context
requestWindowFeature(Window.FEATURE_NO_TITLE)
initView()
}
constructor(context: Activity, layoutResID: Int) : super(context) {
defLayoutResId = layoutResID
activityContext = context
requestWindowFeature(Window.FEATURE_NO_TITLE) requestWindowFeature(Window.FEATURE_NO_TITLE)
initView() initView()
} }
@ -43,7 +52,7 @@ class POPDialog(context: Activity) : Dialog(context) {
private fun initView() { private fun initView() {
mainView = FrameLayout(activityContext) mainView = FrameLayout(activityContext)
view = view =
LayoutInflater.from(context).inflate(R.layout.dialog, mainView, false) as LinearLayout LayoutInflater.from(context).inflate(defLayoutResId, mainView, false)
val animation = AnimationUtils.loadAnimation( val animation = AnimationUtils.loadAnimation(
context, context,
R.anim.enter_anim R.anim.enter_anim
@ -83,11 +92,11 @@ class POPDialog(context: Activity) : Dialog(context) {
*/ */
public fun setMessage(text: String) { public fun setMessage(text: String) {
val textView = TextView(ContextThemeWrapper(activityContext, R.style.messageText)) val textView = TextView(ContextThemeWrapper(activityContext, R.style.messageText))
val params = FrameLayout.LayoutParams( val params = ViewGroup.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT FrameLayout.LayoutParams.MATCH_PARENT
); )
textView.layoutParams = params; textView.layoutParams = params
textView.text = text textView.text = text
dialogContext.addView(textView) dialogContext.addView(textView)
} }
@ -128,7 +137,8 @@ class POPDialog(context: Activity) : Dialog(context) {
* 不允许设置资源id * 不允许设置资源id
* @param layoutResID 正文id * @param layoutResID 正文id
*/ */
@Deprecated("设置资源id会导致内容无法控制不建议使用", @Deprecated(
"设置资源id会导致内容无法控制不建议使用",
ReplaceWith("throw RuntimeException") ReplaceWith("throw RuntimeException")
) )
override fun setContentView(layoutResID: Int) { override fun setContentView(layoutResID: Int) {
@ -191,7 +201,8 @@ class POPDialog(context: Activity) : Dialog(context) {
) )
params.height = (dialogContext.height / 1.25).toInt() params.height = (dialogContext.height / 1.25).toInt()
dialogContext.layoutParams = params dialogContext.layoutParams = params
view.y =(POPDialogUtils.getDisplay(activityContext).mode.physicalHeight / 2).toFloat() view.y =
(POPDialogUtils.getDisplay(activityContext).mode.physicalHeight / 2).toFloat()
expansionY = view.y expansionY = view.y
} }
configTouch() configTouch()
@ -208,8 +219,8 @@ class POPDialog(context: Activity) : Dialog(context) {
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT ViewGroup.LayoutParams.MATCH_PARENT
) )
params.width= (POPDialogUtils.getDisplay(activityContext).mode.physicalWidth*0.8).toInt() params.width = (POPDialogUtils.getDisplay(activityContext).mode.physicalWidth * 0.8).toInt()
params.gravity=Gravity.CENTER params.gravity = Gravity.CENTER
layout.layoutParams = params layout.layoutParams = params
layout.setOnTouchListener(object : View.OnTouchListener { layout.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(p0: View?, p1: MotionEvent?): Boolean { override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {

View File

@ -1,13 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/linearLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout <LinearLayout
android:id="@+id/linearLayout2" android:id="@+id/pop_dialog_title_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/dialog_title" android:background="@drawable/dialog_title"
@ -17,7 +16,7 @@
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<FrameLayout <FrameLayout
android:id="@+id/title" android:id="@+id/pop_dialog_title"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginLeft="15dp" android:layout_marginLeft="15dp"
@ -30,20 +29,20 @@
</LinearLayout> </LinearLayout>
<View <View
android:id="@+id/divider" android:id="@+id/pop_dialog_divider"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="1dp" android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/linearLayout2" app:layout_constraintTop_toBottomOf="@id/pop_dialog_title_layout"
android:background="?android:attr/listDivider" /> android:background="?android:attr/listDivider" />
<FrameLayout <FrameLayout
android:id="@+id/context" android:id="@+id/pop_dialog_content"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="#f1f2f6" android:background="#f1f2f6"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0" app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider" app:layout_constraintTop_toBottomOf="@+id/pop_dialog_divider"
android:orientation="vertical" /> android:orientation="vertical" />
</LinearLayout> </LinearLayout>

View File

@ -18,6 +18,7 @@
</style> </style>
<style name="messageText"> <style name="messageText">
<item name="android:textColor">#212121</item> <item name="android:textColor">#212121</item>
<item name="android:layout_gravity">center</item>
<item name="android:gravity">center</item> <item name="android:gravity">center</item>
<item name="android:padding">20dp</item> <item name="android:padding">20dp</item>
</style> </style>

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/pop_dialog_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:background="@color/purple_200" />
<LinearLayout
android:id="@+id/pop_dialog_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_200"
android:orientation="vertical" />
</LinearLayout>