프로그래밍/Android-Java
[ANDROID] custom toast 커스텀 토스트
채연2
2020. 12. 15. 10:17
토스트를 사용하던 중에 배경이 회색인게 마음에 안들어서 커스텀 토스트를 만들어 보았다.
res/layout/toast_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/custom_toast_layout"
android:background="@drawable/custom_bg"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:id="@+id/custom_toast_msg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#000"
android:layout_margin="20dp"
android:textSize="20sp"
android:text="가나다라마바사"/>
</LinearLayout>
res/drawable/custom_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#FFF" />
<stroke android:width="1dip" android:color="#000" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
위의 코드처럼 layout을 꾸며보았다. 그러면 다음과 같이 나온다 !
java 코드는 다음처럼 작성하면 끝 !
private TextView toast_msg;
private Toast toast;
//-------------------------------------
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_custom, (ViewGroup) findViewById(R.id.custom_toast_layout));
toast_msg = layout.findViewById(R.id.custom_toast_msg);
toast = new Toast(this);
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
//-------------------------------------
toast_msg.setText("커스텀 토스트 입니다.");
toast.show();
320x100