기본 토스트의 사용법 이외에 xml로 구성된 레이아웃을 이용하여 뷰를 토스트에 표시하는 방법이다.
좀 더 깔끔한 UI 작업을 위해 활용가능할 것 같다.
준비물
- ToastTestActivity : 메인액티비티
- customtoast.xml : 토스트 내부에 보여질 레이아웃
- main.xml : 메인 레이아웃
ToastTestActivity
======================================================================================================
package com.pineone;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ToastTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button testBtn = (Button) findViewById(R.id.testBtn);
testBtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = new Toast(ToastTestActivity.this);
LayoutInflater inflater = getLayoutInflater();
View vToast = inflater.inflate(R.layout.customtoast, null);
toast.setView(vToast);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
});
}
}
customtoast.xml
======================================================================================================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/icon"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="Custom Toast Message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
main.xml
======================================================================================================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/testBtn"
android:layout_width="200dip"
android:layout_height="80dip"
android:text="Toast Test"
android:textSize="30dip"
/>
</LinearLayout>
'Program > Android Java' 카테고리의 다른 글
BroadcastReceiver (0) | 2011.10.23 |
---|---|
안드로이드 동영상 스트리밍 재생 (0) | 2011.10.23 |
Android API – 선택유지 리스트뷰 (1) | 2011.10.04 |
Intro화면 코드 (0) | 2011.09.21 |
[Android 2.3] DownloadManager 사용하기 (0) | 2011.09.21 |