怎么改变toast的大小

跑步_跑步 2014-01-17 11:37:37
我自定义了一个toast,改变toast里的布局大小,貌似只能改变高度,宽度的话改小可以,改大就不行了。这是什么原因啊?不能随意改变toast的大小么,可以在代码里改变大小码?
...全文
1356 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_18591009 2015-02-03
  • 打赏
  • 举报
回复
主配置文件中加入 <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:resizeable="true" android:anyDensity="true"/> 就ok了
exaltation 2014-10-16
  • 打赏
  • 举报
回复
引用 20 楼 leirenbaobao 的回复:
[quote=引用 12 楼 fantcy 的回复:] 今天在公司没事 看了看你的问题 别说我也遇到了同样的问题 我不断的尝试终于解决了这个问题 但是我不知道这是为什么 <?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)的方式是无法改变大小的不知道是为什么
此楼是正解。很多朋友还没搞清楚情况。[/quote]RelativeLayout也不行,一定要是LinearLayout
小强就是我 2014-10-15
  • 打赏
  • 举报
回复
引用 12 楼 fantcy 的回复:
今天在公司没事 看了看你的问题 别说我也遇到了同样的问题 我不断的尝试终于解决了这个问题 但是我不知道这是为什么 <?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)的方式是无法改变大小的不知道是为什么
此楼是正解。很多朋友还没搞清楚情况。
wenzs01 2014-08-22
  • 打赏
  • 举报
回复
什么呀什么呀
hyfeng_ccle 2014-08-22
  • 打赏
  • 举报
回复
Toast有一个setMargin() ,试一下,值是0~1之间。
一点一刻 2014-04-16
  • 打赏
  • 举报
回复
你可以看一下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会一直显示,所以你要自己管理(开个线程来记录时间关闭)
跑步_跑步 2014-01-21
  • 打赏
  • 举报
回复
楼上有什么特别的?
MR__P 2014-01-21
  • 打赏
  • 举报
回复
<?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> 楼主拿去用吧
Small_MuMu 2014-01-20
  • 打赏
  • 举报
回复
引用 1 楼 ncepu307 的回复:
调用Toast的setView(View view)方法,这里的view 可以通过inflate() xml文件来获得
Toast t = Toast.makeText(...);
t.setView(yourView);
t.show();
二楼方法行的通
fantcy 2014-01-20
  • 打赏
  • 举报
回复
It only fills the amount of space required for the message and the current activity remains visible and interactive. 今天真是太闲了 我查了下google原文意思是: 它只会填充信息所需要的空间且当前activity要是活动的并且是可见的
fantcy 2014-01-20
  • 打赏
  • 举报
回复
今天在公司没事 看了看你的问题 别说我也遇到了同样的问题 我不断的尝试终于解决了这个问题 但是我不知道这是为什么 <?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)的方式是无法改变大小的不知道是为什么
跑步_跑步 2014-01-18
  • 打赏
  • 举报
回复
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();
		
	}
跑步_跑步 2014-01-18
  • 打赏
  • 举报
回复
引用 7 楼 pengguohua1988 的回复:
[quote=引用 5 楼 anhenzhufeng 的回复:] [quote=引用 4 楼 ncepu307 的回复:] [quote=引用 3 楼 anhenzhufeng 的回复:] [quote=引用 2 楼 lxq_xsyu 的回复:] [quote=引用 1 楼 ncepu307 的回复:] 调用Toast的setView(View view)方法,这里的view 可以通过inflate() xml文件来获得
Toast t = Toast.makeText(...);
t.setView(yourView);
t.show();
这个可以解决[/quote] View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊[/quote] 楼主整那么多参数干嘛?inflate第二个参数直接null。[/quote] 跟这个没有关系吧[/quote]第二个参数写null 还有贴一下你的布局看下吧[/quote] 感谢这么多热心朋友,下面是我的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>
另外我指定最外层的布局的长宽了,还是不行
黑马跨境圈 2014-01-18
  • 打赏
  • 举报
回复
自定义一个TOAST
依然绿茶 2014-01-17
  • 打赏
  • 举报
回复
调用Toast的setView(View view)方法,这里的view 可以通过inflate() xml文件来获得
Toast t = Toast.makeText(...);
t.setView(yourView);
t.show();
失落夏天 版主 2014-01-17
  • 打赏
  • 举报
回复
下面是我写的,几乎完全改写了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();
	}
	
}
MR__P 2014-01-17
  • 打赏
  • 举报
回复
引用 5 楼 anhenzhufeng 的回复:
[quote=引用 4 楼 ncepu307 的回复:] [quote=引用 3 楼 anhenzhufeng 的回复:] [quote=引用 2 楼 lxq_xsyu 的回复:] [quote=引用 1 楼 ncepu307 的回复:] 调用Toast的setView(View view)方法,这里的view 可以通过inflate() xml文件来获得
Toast t = Toast.makeText(...);
t.setView(yourView);
t.show();
这个可以解决[/quote] View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊[/quote] 楼主整那么多参数干嘛?inflate第二个参数直接null。[/quote] 跟这个没有关系吧[/quote]第二个参数写null 还有贴一下你的布局看下吧
依然绿茶 2014-01-17
  • 打赏
  • 举报
回复
你就直接把R.layout.toast_layout 这个xml的布局宽度和高度各设置成100,然后inflate第二个参数为null,试试,不尝试你怎么知道?
跑步_跑步 2014-01-17
  • 打赏
  • 举报
回复
引用 4 楼 ncepu307 的回复:
[quote=引用 3 楼 anhenzhufeng 的回复:] [quote=引用 2 楼 lxq_xsyu 的回复:] [quote=引用 1 楼 ncepu307 的回复:] 调用Toast的setView(View view)方法,这里的view 可以通过inflate() xml文件来获得
Toast t = Toast.makeText(...);
t.setView(yourView);
t.show();
这个可以解决[/quote] View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊[/quote] 楼主整那么多参数干嘛?inflate第二个参数直接null。[/quote] 跟这个没有关系吧
依然绿茶 2014-01-17
  • 打赏
  • 举报
回复
引用 3 楼 anhenzhufeng 的回复:
[quote=引用 2 楼 lxq_xsyu 的回复:] [quote=引用 1 楼 ncepu307 的回复:] 调用Toast的setView(View view)方法,这里的view 可以通过inflate() xml文件来获得
Toast t = Toast.makeText(...);
t.setView(yourView);
t.show();
这个可以解决[/quote] View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊[/quote] 楼主整那么多参数干嘛?inflate第二个参数直接null。
加载更多回复(2)

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧