调用Toast的setView(View view)方法,这里的view 可以通过inflate() xml文件来获得
Toast t = Toast.makeText(...);
t.setView(yourView);
t.show();
Toast t = Toast.makeText(...);
t.setView(yourView);
t.show();
解决方案 »
- 通过广播通知获得最新一条短信内容,内容不全
- android httpclient中 HttpGet问题。
- Android2.x ~ 3.0范围内关于USB事件的监听处理
- 如何动态修改按钮的style属性?
- 菜鸟咨询一个Android开发入门的问题
- avd启动后进不去,停留在开机画面
- 初进公司实习 最近要开发安卓手机界面 做出九宫格那样的菜单 主菜单下还有子菜单 零基础怎么办有什么资料或视屏可以看的 求大神指点
- eclipse中 添加依赖包报错
- android studio app开发
- Android 编译啥的都没问题 但app就是运行不起来 求大佬看看
- 【紧急求助】有没有比dex2jar更好用的工具?
- arm-linux-androideabi-g++: error: CreateProcess: No such file or directory
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊
楼主整那么多参数干嘛?inflate第二个参数直接null。
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊
楼主整那么多参数干嘛?inflate第二个参数直接null。
跟这个没有关系吧
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)((Activity)context).findViewById(R.id.toast_layout_root));我就是通过这个来设置的,把toast_layout_root的宽度变长不行,变短可以。很纠结啊
楼主整那么多参数干嘛?inflate第二个参数直接null。
跟这个没有关系吧第二个参数写null
还有贴一下你的布局看下吧
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();
}
}
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>另外我指定最外层的布局的长宽了,还是不行
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();
}
我不断的尝试终于解决了这个问题 但是我不知道这是为什么
<?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)的方式是无法改变大小的不知道是为什么
今天真是太闲了 我查了下google原文意思是:
它只会填充信息所需要的空间且当前activity要是活动的并且是可见的
<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>
楼主拿去用吧
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会一直显示,所以你要自己管理(开个线程来记录时间关闭)
此楼是正解。很多朋友还没搞清楚情况。RelativeLayout也不行,一定要是LinearLayout
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:resizeable="true"
android:anyDensity="true"/> 就ok了