新增自定义布局支持,可参考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
.gradle
/local.properties
/.idea
/.idea/caches
/.idea/libraries
/.idea/modules.xml

View File

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

View File

@ -1,13 +1,12 @@
<?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:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout2"
android:id="@+id/pop_dialog_title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_title"
@ -17,7 +16,7 @@
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:id="@+id/title"
android:id="@+id/pop_dialog_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="15dp"
@ -30,20 +29,20 @@
</LinearLayout>
<View
android:id="@+id/divider"
android:id="@+id/pop_dialog_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintTop_toBottomOf="@id/linearLayout2"
app:layout_constraintTop_toBottomOf="@id/pop_dialog_title_layout"
android:background="?android:attr/listDivider" />
<FrameLayout
android:id="@+id/context"
android:id="@+id/pop_dialog_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f1f2f6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider"
app:layout_constraintTop_toBottomOf="@+id/pop_dialog_divider"
android:orientation="vertical" />
</LinearLayout>

View File

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