안드로이드 시스템에서 얼럿 창 역할을 수행하는 Toast 객체에 대한 커스터마이징 소스 셈플이다. 디자인 소스를 XML 형태로 생성한 뒤 Toast 객체에 입혀주는 형식이다.

 

 

 

# MainActivity.java

package com.example.wookoa.wookoa_13;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = getLayoutInflater();
View toastDesign = inflater.inflate(R.layout.toast_design, (ViewGroup)findViewById(R.id.toast_design_root)); //toast_design.xml 파일의 toast_design_root 속성을 로드

TextView text = toastDesign.findViewById(R.id.TextView_toast_design);
text.setText("WellCome to Wookoa!"); // toast_design.xml 파일에서 직접 텍스트를 지정 가능

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0); // CENTER를 기준으로 0, 0 위치에 메시지 출력
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastDesign);
toast.show();
}
});
}
}

 

 

# res/layout/toast.design.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/toast_design_root">

<TextView
android:id="@+id/TextView_toast_design"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#ffffff"
android:background="@drawable/toast_design_bg" />
</LinearLayout>

 

 

# res/drawable/toast_design_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<stroke
android:width="3dp"
android:color="#BDBDBD" />
<solid
android:color="#8C8C8C" />
<padding
android:left="20dp"
android:bottom="20dp"
android:right="20dp"
android:top="20dp" />
<corners
android:radius="15dp" />
</shape>

 

 

Private comment
Prev 1 2 3 4 5 6 7 8 9 10 Next