상세 컨텐츠

본문 제목

[Android]Toast - 02

Android 개발

by mobile 2012. 8. 15. 12:29

본문

반응형

▷ SampleToastShapeActivity.java

import android.app.Activity;

import android.os.Bundle;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;


public class SampleToastShapeActivity extends Activity {


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        Button showBtn = (Button) findViewById(R.id.showBtn);

        showBtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

        LayoutInflater inflater = getLayoutInflater();

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

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

       

        Toast toast = new Toast(getApplicationContext());

        text.setText("Hello Android !");

        toast.setGravity(Gravity.CENTER, 0, 0);

        toast.setDuration(Toast.LENGTH_SHORT);

       

        toast.setView(layout);

        toast.show();

        }

        });

    }

}


▷ res/layout/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/showBtn"

   android:layout_width="wrap_content" 

   android:layout_height="wrap_content" 

   android:layout_gravity="center"

   android:layout_marginTop="30dp"

   android:text="Show"

   />

</LinearLayout>


▷ res/layout/toastborder.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/toast_layout_root"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="10dp"

>

<TextView

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:padding="20dp"

android:background="@drawable/toast"

/>

</LinearLayout>


▷ res/drawable/toast.xml

<?xml version="1.0" encoding="UTF-8" ?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"

android:shape="rectangle"

>

<stroke

android:width="4dp" 

android:color="#ffffff00" 

/>

<solid 

android:color="#ff883300" 

/>

<padding 

android:left="20dp" 

android:top="20dp"

android:right="20dp" 

android:bottom="20dp" 

/>

<corners 

android:radius="15dp" 

/>

</shape>


▷ 결과



반응형

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

[Android]event - 02  (0) 2012.08.15
[Android]이벤트 - 01  (0) 2012.08.15
[Android]Toast - 01  (0) 2012.08.15
[Android] 리소스와 메니페스트  (0) 2012.08.14
[Android]브로드캐스트 수신자  (0) 2012.08.12

관련글 더보기

댓글 영역