우리 회사는 회사 전용 글꼴을 써야하기 때문에 custom toast를 따로 만들어주어야 한다!
그런데 전에 쓰던 custom toast가 targestSdk를 30 으로 올리면서 안먹힌다 ㅜㅜ
보니까 android11 부터는 Toast.setView()가 deprcated 된다고하넹
대신에 Snackbar를 쓰라나? 모라나 ,,
하여튼! 어찌저찌 해보니 되긴 된다!
나중에 다시 한번 수정을 해야할 듯
우선 코틀린!
데이터 바인딩을 해줘서 코드가 약간 다를 것이야
* ToastUtil.kt
fun showToast(context: Context, message: String) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).also {
val inflater = LayoutInflater.from(context)
val binding: CustomToastBinding =
DataBindingUtil.inflate(inflater, R.layout.custom_toast, null, false)
if (getTypeface(context) != null) {
binding.toastText.typeface = getTypeface(context)
} else {
binding.toastText.typeface = ResourcesCompat.getFont(context, R.font.fontName)
}
binding.toastText.text = message
it.view = binding.root
}.show()
}
* ToastUtil.java
public static void showToast(Context c, String message) {
if (c == null || TextUtils.isEmpty(message)) {
return;
}
Toast mToast = Toast.makeText(c, message, Toast.LENGTH_SHORT);
LayoutInflater inflater = (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_toast, null);
TextView toast_msg = (TextView) layout.findViewById(R.id.toast_text);
toast_msg.setTypeface(RuntimeEnv.getInstance(DashcamApplication.getContext()).getTypeface());
toast_msg.setText(message);
mToast.setView(layout.getRootView());
mToast.show();
}
* custom_toast.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
LinearLayout
android:id="@+id/custom_toast_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/custom_toast_background"
android:orientation="horizontal"
android:padding="8dp">
TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="13dp">
/TextView>
/LinearLayout>
/LinearLayout
근데.. " < " 요거 넣으면 왜 코드가 안보이지 ..? 우선 없애버려ㅜㅋㅋㅋ
나는 다시 일하러 총총총........
'android' 카테고리의 다른 글
[Android][Kotlin]Canvas: trying to draw too large , 비트맵 이슈 (0) | 2021.05.25 |
---|