토스트를 사용하던 중에 배경이 회색인게 마음에 안들어서 커스텀 토스트를 만들어 보았다.
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
'프로그래밍 > Android-Java' 카테고리의 다른 글
[ANDROID] java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() (0) | 2020.12.15 |
---|---|
[ANDROID] byteArray to String (0) | 2020.12.15 |
[ANDROID] Dialog 밖의 어두운 배경 없애기 (0) | 2020.12.14 |
[ANDROID] java.lang.IllegalArgumentException: Comparison method violates its general contract! (0) | 2020.12.04 |
[ANDROID] 두 점의 각도 구하기 (0) | 2020.11.17 |
댓글