80,360
社区成员
发帖
与我相关
我的任务
分享
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"?>
<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>
另外我指定最外层的布局的长宽了,还是不行Toast t = Toast.makeText(...);
t.setView(yourView);
t.show();
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();
}
}