1. Toast
간단한 메시지를 잠깐 보여주었다가 없어지는 뷰로, 애플리케이션 위에 떠 있는 뷰라 할 수 있다.
Toast.makeText(Context context, String message, int duration)
public void setGravity(int gravity, int xOffset, int yOffset)
public void setMargin(float horizontalMargin, float verticalMargin)
2. 예제1 - 결과 화면
3. 예제1 - MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.tistory.qlyh8.pracitice; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast toast = Toast.makeText(getApplicationContext(), "위치가 바뀐 토스트", Toast.LENGTH_LONG); toast.setGravity(Gravity.TOP|Gravity.LEFT, 100, 200); toast.show(); } }); } } | cs |
4. 예제2- 결과 화면
5. 예제2 - drawable/toast.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:color="#295986" android:width="10dp"/> <solid android:color="#eac35f"/> <padding android:left="20dp" android:right="20dp" android:top="20dp" android:bottom="20dp"/> <corners android:radius="15dp"/> </shape> | cs |
6. 예제2 - toast_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?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="horizontal" android:id="@+id/toast_layout_root"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20dp" android:textSize="20sp" android:background="@drawable/toast"/> </LinearLayout> | cs |
7. 예제2 - MainActivty.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package com.tistory.qlyh8.pracitice; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; 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 { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)findViewById(R.id.toast_layout_root)); TextView textView = layout.findViewById(R.id.textView); textView.setText("모양이 바뀐 토스트"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER, 0, -100); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); } }); } } | cs |
출처: https://www.edwith.org/boostcourse-android/lecture/17052/
'Android' 카테고리의 다른 글
AlertDialog (알림 대화상자) (0) | 2018.04.28 |
---|---|
Snackbar (스낵바) (0) | 2018.04.28 |
Event (이벤트) - 키 이벤트 (0) | 2018.04.28 |
Event (이벤트) - 터치와 제스처 이벤트 (0) | 2018.04.28 |
Event (이벤트) (0) | 2018.04.28 |