Android - Kotlin 확장 기능이 있는 커스텀 토스트
1단계: 새 프로젝트 만들기
먼저 새로운 Android Studio 프로젝트를 만듭니다.
 2단계: 레이아웃 만들기
두 번째 단계는 매우 간단한 레이아웃을 만드는 것입니다. 우리의 경우에는 EditText 및 Button 만 포함합니다.
아래 코드를 복사하여 activity_main.xml 파일에 붙여넣습니다.
 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/et_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_margin="24dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="0.3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />
    <Button
        android:id="@+id/btn_customToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Custom Toast"
        app:layout_constraintTop_toBottomOf="@+id/btn_defaultToast"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
 3단계: 아이콘 만들기
Toast 메시지에도 아이콘이 있습니다. 따라서 drawable 폴더에 아래 세부 정보로 체크 표시를 만듭니다.
두 번째 단계는 매우 간단한 레이아웃을 만드는 것입니다. 우리의 경우에는
EditText 및 Button 만 포함합니다.아래 코드를 복사하여
activity_main.xml 파일에 붙여넣습니다.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <EditText
        android:id="@+id/et_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_margin="24dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="0.3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />
    <Button
        android:id="@+id/btn_customToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Custom Toast"
        app:layout_constraintTop_toBottomOf="@+id/btn_defaultToast"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
3단계: 아이콘 만들기
Toast 메시지에도 아이콘이 있습니다. 따라서 drawable 폴더에 아래 세부 정보로 체크 표시를 만듭니다.
colors.xml 파일에 색상을 생성합니다. #78C257 4단계: 모양 만들기
맞춤 토스트 메시지에는
따라서
drawable 폴더에 새 도형을 만들고 아래 줄을 붙여넣습니다.shape_roundedcorners
<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android">
    <solid
        android:color="@color/light_green"/>
    <corners
        android:radius="18dp"/>
    <stroke
        android:color="@color/green"
        android:width="3dp"/>
</shape>
light_green 색상의 16진수 코드: #B9D7AC5단계: 레이아웃 만들기
다음은 Toast 메시지에 대한 사용자 정의 레이아웃을 만드는 것입니다. 따라서 res > layout라는 이름으로 custom_toast 폴더에 새 레이아웃 파일을 만듭니다. 그런 다음 아래 줄을 붙여 넣으십시오.
 custom_toast
<LinearLayout
    android:id="@+id/cl_customToastContainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="12dp"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="@drawable/shape_roundedcorners">
    <ImageView
        android:id="@+id/iv_done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_done_green" />
    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mission Completed"
        android:textColor="@color/green"
        android:layout_marginStart="6dp" />
</LinearLayout>
 6단계: 확장 기능 만들기
이 단계에서는 먼저 새 패키지와 파일을 만들어야 합니다.
custom_toast
<LinearLayout
    android:id="@+id/cl_customToastContainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="12dp"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="@drawable/shape_roundedcorners">
    <ImageView
        android:id="@+id/iv_done"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_done_green" />
    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mission Completed"
        android:textColor="@color/green"
        android:layout_marginStart="6dp" />
</LinearLayout>
이 단계에서는 먼저 새 패키지와 파일을 만들어야 합니다.
util ViewExt.kt 아래 코드를 복사하여 새
ViewExt.kt 파일에 붙여넣습니다.Extension function of custom Toast 
fun Toast.showCustomToast(message: String, activity: Activity)
{
    val layout = activity.layoutInflater.inflate (
        R.layout.custom_toast,
        activity.findViewById(R.id.cl_customToastContainer)
    )
    val textView = layout.findViewById<TextView>(R.id.tv_message)
    textView.text = message
    this.apply {
        setGravity(Gravity.BOTTOM, 0, 40)
        duration = Toast.LENGTH_LONG
        view = layout
        show()
    }
}
메모
LayoutInflater를 사용하여 레이아웃TextView의 텍스트를 설정합니다.apply 범위 함수를 호출하여 중력, 기간 및 보기를 설정할 수 있습니다. 7단계: 클릭 리스너 설정
마지막 단계는 Toast를 클릭할 때 사용자 지정 Button 메시지를 표시하는 것입니다. 따라서 MainActivity를 열고 아래 코드를 onCreate() 메서드에 붙여넣습니다.
 
btn_customToast 
btn_customToast.setOnClickListener {_button ->
    Toast(this).showCustomToast (
        et_message.text.toString().trim(),
        this
    )
}
 앱 실행
앱을 실행하고 테스트합니다. ;)
 자세한 내용은
전체 자습서는 내 웹 사이트에서 사용할 수 있습니다.
 Custom Toast - Inspire Coding - Android development in Kotlin
그리고 GitHub에서도 소스코드를 확인할 수 있습니다.
 GitHub
 좋은 하루 되세요 :)
 더 많은 안드로이드 튜토리얼
이와 같은 더 많은 Android 자습서를 수행하려면 내 웹 사이트를 방문하십시오.
 
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(Android - Kotlin 확장 기능이 있는 커스텀 토스트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://dev.to/inspire_coding/android-custom-toast-with-kotlin-extension-function-23pj
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
btn_customToast 
btn_customToast.setOnClickListener {_button ->
    Toast(this).showCustomToast (
        et_message.text.toString().trim(),
        this
    )
}
앱을 실행하고 테스트합니다. ;)
자세한 내용은
전체 자습서는 내 웹 사이트에서 사용할 수 있습니다.
 Custom Toast - Inspire Coding - Android development in Kotlin
그리고 GitHub에서도 소스코드를 확인할 수 있습니다.
 GitHub
 좋은 하루 되세요 :)
 더 많은 안드로이드 튜토리얼
이와 같은 더 많은 Android 자습서를 수행하려면 내 웹 사이트를 방문하십시오.
 
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(Android - Kotlin 확장 기능이 있는 커스텀 토스트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://dev.to/inspire_coding/android-custom-toast-with-kotlin-extension-function-23pj
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
더 많은 안드로이드 튜토리얼
이와 같은 더 많은 Android 자습서를 수행하려면 내 웹 사이트를 방문하십시오.
                Reference
이 문제에 관하여(Android - Kotlin 확장 기능이 있는 커스텀 토스트), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/inspire_coding/android-custom-toast-with-kotlin-extension-function-23pj텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)