调用Toast的setView(View view)方法,这里的view 可以通过inflate() xml文件来获得
Toast t = Toast.makeText(...);
t.setView(yourView);
t.show();

解决方案 »

  1.   


     View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊
      

  2.   


     View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊
    楼主整那么多参数干嘛?inflate第二个参数直接null。
      

  3.   


     View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊
    楼主整那么多参数干嘛?inflate第二个参数直接null。
    跟这个没有关系吧
      

  4.   

    你就直接把R.layout.toast_layout  这个xml的布局宽度和高度各设置成100,然后inflate第二个参数为null,试试,不尝试你怎么知道?
      

  5.   


     View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊
    楼主整那么多参数干嘛?inflate第二个参数直接null。
    跟这个没有关系吧第二个参数写null
    还有贴一下你的布局看下吧
      

  6.   

    下面是我写的,几乎完全改写了toast了public class ZMToast extends Toast{
    Context context;
    TextView content;
    RelativeLayout mainLayout;

    public ZMToast(Context context,int textsize) {
    super(context);
    if (context == null) {
    return;
    }
    this.context=context;

    mainLayout = new RelativeLayout(context);
    ViewGroup.LayoutParams mainP = new ViewGroup.LayoutParams(
    ViewGroup.LayoutParams.FILL_PARENT,
    ViewGroup.LayoutParams.WRAP_CONTENT);
    mainLayout.setLayoutParams(mainP);

    content=new TextView(context);

    setDuration(Toast.LENGTH_LONG);
    setView(mainLayout);
    }


    public void showLonger() {
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() { @Override
    public void run() {
    show();
    } }, 0); } public void showLonger(Handler handler) {
    handler.postDelayed(new Runnable() { @Override
    public void run() {
    show();
    } }, 0);
    } public int getTextLength(Context context,int width){
    int screenwidth = PhoneUtil.getResolution(context)[0];
    return (screenwidth*width)/720;
    }


    public int getTextSize(Context context,int size){
    int screenwidth = PhoneUtil.getResolution(context)[0];
    return (screenwidth*size)/720;
    }

    public void setBackGround(){
    try {
    InputStream open = context.getResources().getAssets()
    .open("toast.png");
    Drawable background = Drawable.createFromStream(open, "toast.png");
    mainLayout.setBackgroundDrawable(background);
    } catch (Exception e) {
    mainLayout.setBackgroundColor(Color.BLACK);
    }
    }

    public void setAnim(){
    TranslateAnimation alphaAnimation2 = new TranslateAnimation(-20f, 0, 0,
    0);
    alphaAnimation2.setDuration(90);
    alphaAnimation2.setRepeatCount(9);
    alphaAnimation2.setRepeatMode(Animation.REVERSE);
    content.setAnimation(alphaAnimation2);
    alphaAnimation2.start();
    }

    }
      

  7.   


     View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊
    楼主整那么多参数干嘛?inflate第二个参数直接null。
    跟这个没有关系吧第二个参数写null
    还有贴一下你的布局看下吧
    感谢这么多热心朋友,下面是我的xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >    <RelativeLayout 
            android:id="@+id/toast_layout_root"
        android:layout_width="461dp"
        android:layout_height="121dp"  
        android:background="@drawable/bg_toast"
        android:visibility="gone"   >
        <LinearLayout         
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="44dp"
            android:layout_marginBottom="8dp" >
            <TextView 
                android:id="@+id/t_num"
                android:textSize="26sp"
                android:textColor="@color/txt_color3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:gravity="right"
                android:layout_gravity="bottom" />
            <com.tcl.globlelive.view.AlwaysMarqueeTextView
                android:id="@+id/t_name"
                android:textSize="22sp"
                android:textColor="@color/txt_color0"
                android:layout_width="120dp" 
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:gravity="center"
                android:layout_gravity="bottom"
                android:singleLine="true"  
            android:ellipsize="marquee"               
         android:marqueeRepeatLimit="marquee_forever"
         android:scrollHorizontally="true"
         android:focusable="true" />
            <TextView
                android:id="@+id/t_date"
                android:textSize="22sp"
                android:textColor="@color/color7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:layout_marginLeft="10dp"
                android:layout_gravity="bottom"/>
        </LinearLayout>
        <LinearLayout 
            android:orientation="horizontal"
            android:gravity="bottom"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="48dp" >
            <TextView 
             android:id="@+id/t_epg1_time"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"  
            android:layout_marginLeft="17dp" 
            android:textSize="18sp"
            android:textColor="@color/txt_black"
             android:gravity="left" />
         <com.tcl.globlelive.view.AlwaysMarqueeTextView
             android:id="@+id/t_epg1_name"
             android:layout_height="wrap_content"
             android:layout_width="260dip"
             android:layout_marginLeft="40dp"
             android:textSize="18sp"
            android:textColor="@color/txt_black"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:ellipsize="marquee"
             android:gravity="left" />        
        </LinearLayout>
        <LinearLayout 
            android:orientation="horizontal"
            android:gravity="bottom"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="80dp" >
            <TextView 
             android:id="@+id/t_epg2_time"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"  
            android:layout_marginLeft="17dp" 
            android:textSize="18sp"
            android:textColor="@color/txt_black"
             android:gravity="left" />
         <com.tcl.globlelive.view.AlwaysMarqueeTextView 
             android:id="@+id/t_epg2_name"
             android:layout_height="wrap_content"
             android:layout_width="260dp"
             android:layout_marginLeft="40dp"
             android:textSize="18sp"
            android:textColor="@color/txt_black"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:ellipsize="marquee"
             android:gravity="left" />
        </LinearLayout>
    </RelativeLayout>
        <RelativeLayout 
        android:id="@+id/toast_layout_root0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" >
        <LinearLayout 
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="110dip" 
            android:background="@drawable/bg_info_toast">
    <ImageView 
        android:src="@drawable/tcl_info"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:layout_marginLeft="10dip" />
            <TextView 
                android:id="@+id/txt" 
                android:textSize="14sp"
                android:textColor="@color/txt_black"
                android:textStyle="italic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:layout_marginLeft="20dip" />
        </LinearLayout>
    </RelativeLayout>
    <RelativeLayout 
        android:id="@+id/toast_layout_root1"
        android:layout_width="wrap_content"
        android:layout_height="80dp" >
            <TextView
                android:text="@string/txt17"  
                android:textSize="18sp"
                android:textColor="@color/txt_black"
                android:textStyle="bold|italic"
                android:layout_width="342dp"
                android:layout_height="70dp"
                android:paddingLeft="20dp"
                android:layout_marginRight="35dp"
                android:layout_marginLeft="45dp"
                android:paddingRight="10dp"
                android:gravity="left|center_vertical"
        android:background="@drawable/bg_info_toast1"/>
    </RelativeLayout>
    </RelativeLayout>另外我指定最外层的布局的长宽了,还是不行
      

  8.   

    java代码中是这样写的
    static public void showChannelInfo(Context context,String[] detail){
        if(sToast == null){
            sToast = new Toast(context.getApplicationContext());
        }
        
            TextView[] texts = new TextView[7];
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            View layout = inflater.inflate(R.layout.toast_layout, null);
            layout.findViewById(R.id.toast_layout_root).setVisibility(View.VISIBLE);
            layout.findViewById(R.id.toast_layout_root0).setVisibility(View.GONE);
            layout.findViewById(R.id.toast_layout_root1).setVisibility(View.GONE);
            texts[0] = (TextView)layout.findViewById(R.id.t_num);
            texts[1] = (TextView)layout.findViewById(R.id.t_name);
            texts[2] = (TextView)layout.findViewById(R.id.t_epg1_time);
            texts[3] = (TextView)layout.findViewById(R.id.t_epg1_name);
            texts[4] = (TextView)layout.findViewById(R.id.t_epg2_time);
            texts[5] = (TextView)layout.findViewById(R.id.t_epg2_name);
            texts[6] = (TextView)layout.findViewById(R.id.t_date);
            int tmp = 370;
            if(context.getResources().getDisplayMetrics().densityDpi==160)
                tmp = 270;
            sToast.setGravity(Gravity.CENTER, 0, tmp);
            sToast.setDuration(Toast.LENGTH_LONG);
    //      setBackground(layout);
            sToast.setView(layout);
        
            if(detail.length !=7 )
                return;
            int i = 0;
            for(String str:detail)
            {
                texts[i].setText(str);
                i++;
            }        sToast.show();

    }
      

  9.   

    今天在公司没事 看了看你的问题 别说我也遇到了同样的问题
    我不断的尝试终于解决了这个问题 但是我不知道这是为什么
    <?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"
        android:gravity="center_horizontal" 
        android:background="#113344">
    <LinearLayout 
        android:layout_width="200dp"
        android:layout_height="200dp"

        android:orientation="vertical"
        android:gravity="center_horizontal" 
        android:background="#113344">
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你好" 
            android:textColor="#111111"
            android:textSize="60pt"
            />
        </LinearLayout>
        </LinearLayout>
    上面红色部分就是我写的xml我发现其实只要改变布局中linearlayout的内部元素的大小即可改变toast的大小
    toast似乎有这样一个机制就是它会自适应内部元素的大小内部大它就大内部元素小它就小 但是具体这是为什么我不知道 看来要查查文档了
    综上所述你的xml
    最好这样写:
    用一个布局将所有的元素包起来(注意这个布局不能使顶层布局要是第二个)然后改变这个布局的大小(你想改多大改多大)然后就可以了
    经本人试验 这个toast用setlayoutparams(params)的方式是无法改变大小的不知道是为什么
      

  10.   

    It only fills the amount of space required for the message and the current activity remains visible and interactive.
    今天真是太闲了 我查了下google原文意思是:
    它只会填充信息所需要的空间且当前activity要是活动的并且是可见的
      

  11.   

    <?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="wrap_content"
        android:orientation="horizontal" >    <TextView
            android:id="@+id/tv_toast"
            android:layout_width="wrap_content"
            android:layout_height="24dip"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:paddingLeft="3dip"
            android:paddingRight="3dip"
            android:background="@drawable/toast_bg"
            android:shadowColor="@android:color/white"
            android:shadowDx="1.0"
            android:shadowDy="1.0"
            android:textColor="#ffffff"
            android:textSize="14dip" /></LinearLayout>
    楼主拿去用吧
      

  12.   

    你可以看一下android toast 的源码他有一个内部私有类,toast 当调用show()方法后会执行   这个内部类TN的构造方法关键不份如下:
    WindowManager   mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);         WindowManager.LayoutParams toastParams=new WindowManager.LayoutParams();
             final WindowManager.LayoutParams paramsAA = toastParams;
             paramsAA.height = WindowManager.LayoutParams.WRAP_CONTENT;
             paramsAA.width = WindowManager.LayoutParams.MATCH_PARENT;

             paramsAA.format = PixelFormat.TRANSLUCENT;
             paramsAA.windowAnimations = R.style.commonFIFOAnimation;
             paramsAA.type = WindowManager.LayoutParams.TYPE_TOAST;
             paramsAA.setTitle("Toast");
             paramsAA.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                     | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                     | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
             mWM.addView(v,toastParams);这里不要调用toast.show()方法
    但是执行这些代码后toast会一直显示,所以你要自己管理(开个线程来记录时间关闭)
      

  13.   

    Toast有一个setMargin() ,试一下,值是0~1之间。
      

  14.   


    此楼是正解。很多朋友还没搞清楚情况。RelativeLayout也不行,一定要是LinearLayout
      

  15.   

    主配置文件中加入<supports-screens  
             android:smallScreens="true"  
             android:normalScreens="true"  
             android:largeScreens="true"  
             android:resizeable="true"  
             android:anyDensity="true"/>  就ok了