灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2871回复:0

Android自定义Toast

楼主#
更多 发布于:2013-01-10 15:39
mainActivity如下:
 
[java]
package cn.c;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.Toast;  
//自定义Toast  
public class MainActivity extends Activity {  
    private Button mButton;  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        mButton=(Button) findViewById(R.id.button);  
        mButton.setOnClickListener(new ClickListenerImpl());  
    }  
    private class ClickListenerImpl implements OnClickListener{  
        public void onClick(View v) {  
            Toast toast=Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT);  
            View toastView=  
            LayoutInflater.from(MainActivity.this).inflate(R.layout.toast, null);  
            toast.setView(toastView);  
            toast.show();  
        }  
          
    }  
}  
 
main.xml如下:
 
[html]  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  
  
   <Button  
       android:id="@+id/button"  
       android:layout_width="150dip"  
       android:layout_height="50dip"  
       android:text="弹出Toast"  
       android:layout_alignParentTop="true"  
       android:layout_centerHorizontal="true"  
   />  
  
</RelativeLayout>  
 
toast.xml如下:
 
[html]  
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="wrap_content"  
    android:layout_height="match_parent"  
    android:orientation="horizontal" >  
    <ImageView  
       android:id="@+id/imageView"  
       android:layout_width="50dip"  
       android:layout_height="40dip"  
       android:src="@drawable/ic_launcher"  
    />  
    <TextView  
       android:id="@+id/textView"  
       android:layout_width="wrap_content"  
       android:layout_height="40dip"  
       android:text="自定义的Toast"  
       android:gravity="center"  
    />  
  
</LinearLayout>  
 

喜欢0 评分0
游客

返回顶部