상세 컨텐츠

본문 제목

[Android]사용자가 만든 토스트

Android 개발

by mobile 2013. 9. 15. 20:13

본문

반응형

custom toast 완성 화면

// 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="match_parent"    

    android:layout_height="match_parent"    

    android:paddingTop="10px">     

    <Button android:id="@+id/button"        

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"        

    android:text="Toast"        

    android:onClick="buttonClicked">     

    </Button> 

</LinearLayout> 


// toast.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="horizontal"

    android:id="@+id/toast_layout_root"

    android:paddingRight="10dp"

    android:paddingLeft="10dp"

    android:layout_marginLeft="10dp"

    android:layout_marginRight="10dp" >

    <LinearLayout 

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="horizontal"

        android:background="#DAAA"

        android:paddingTop="5dp"

        android:paddingBottom="5dp">

        <ImageView android:id="@+id/toastImage"

            android:layout_width="wrap_content"

            android:layout_height="match_parent"

            android:src="@drawable/ic_launcher"

            android:layout_marginRight="10dp"/>

        <TextView android:id="@+id/toastText"

            android:layout_width="wrap_content"

            android:layout_height="match_parent"

            android:textSize="7pt"

            android:textStyle="bold"

            android:layout_gravity="center_vertical"

            android:gravity="center"

            android:layout_marginRight="10dp" />

    </LinearLayout>


</LinearLayout>


// CustomToast.java

package com.example.customtoast;


import android.app.Activity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import android.widget.Toast;


public class CustomToast extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

public void buttonClicked(View view) {

LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.toast, (ViewGroup)findViewById(R.id.toast_layout_root));

TextView text = (TextView)layout.findViewById(R.id.toastText);

text.setText("안녕하세요! 제가 만든 토스트");

Toast toast = new Toast(getApplicationContext());

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(layout);

toast.show();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.custom_toast, menu);

return true;

}

}




반응형

'Android 개발' 카테고리의 다른 글

[Android]핸들러 클래스  (0) 2013.09.19
[Android]스레드 구현  (0) 2013.09.18
[Android]Menu 인플레이터  (0) 2013.09.15
[Android]액티비티와 태스크  (0) 2013.09.09
[Android] 인텐트 - 03  (0) 2013.08.01

관련글 더보기

댓글 영역