帶圖片的Toast 與LinearLayout View

帶圖片的Toast 與LinearLayout View

package irdc.ex05_07;

import android.app.Activity;
import android.os.Bundle;
import android.text.util.Linkify;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class EX05_07 extends Activity
{
private Button mButton01;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mButton01 = (Button)findViewById(R.id.myButton1);
   
    /*設置Button用OnClickListener啓動事件*/
    mButton01.setOnClickListener(new Button.OnClickListener()
    {
     
      @Override
      public void onClick(View v)
      {
        // TODO Auto-generated method stub
       
        /* 創建ImageView */
        ImageView mView01 = new ImageView(EX05_07.this);
        TextView mTextView = new TextView(EX05_07.this);
       
        /* 創建LinearLayout對象 */
        LinearLayout lay = new LinearLayout(EX05_07.this);
       
        /* 設置mTextView去抓取string值 */
        mTextView.setText(R.string.app_url);
       
        /* 判斷mTextView的內容爲何,並與系統做連接 */
        Linkify.addLinks
        (
          mTextView,Linkify.WEB_URLS|
          Linkify.EMAIL_ADDRESSES|
          Linkify.PHONE_NUMBERS
        );
       
        /*用Toast方式顯示*/
        Toast toast = Toast.makeText
                      (
                        EX05_07.this,
                        mTextView.getText(),
                        Toast.LENGTH_LONG
                      );
       
        /* 自定義View對象 */
        View textView = toast.getView();
       
        /* 以水平方式排列 */
        lay.setOrientation(LinearLayout.HORIZONTAL);
       
        /* 在ImageView Widget裏指定顯示的圖片 */
        mView01.setImageResource(R.drawable.icon);
       
        /* 在Layout裏添加剛創建的View */
        lay.addView(mView01);
       
        /* 在Toast裏顯示文字 */
        lay.addView(textView);
       
        /* 以Toasr,setView方法將Layout傳入 */
        toast.setView(lay);
       
        /* 顯示Toast */
        toast.show();
      }     
    });
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章