Toast 圖片提示功能

JAVA:

package com.example.test;


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


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button bn = (Button)findViewById(R.id.bn);
bn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView text = new TextView(MainActivity.this);
text.setText("這是一個圖片提醒");
ImageView im = new ImageView(MainActivity.this);
im.setBackgroundResource(R.drawable.aa);
LinearLayout la = new LinearLayout(MainActivity.this);

Toast toast = Toast.makeText(MainActivity.this, text.getText(), Toast.LENGTH_SHORT);

View view = toast.getView();
la.setOrientation(LinearLayout.HORIZONTAL);
la.addView(im);
la.addView(view);
toast.setView(la);
toast.show();
}
});
}
}


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="vertical" >

    <TextView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
        />
    
    <Button 
        android:id="@+id/bn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圖片提示"
        />


</LinearLayout>

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