您的位置:首页 > 移动开发 > Android开发

android中自定义toast样式

2016-01-25 00:00 519 查看
摘要: 修改toast的背景及出现位置

package cn.juzhong.view.widget;

import cn.juzhong.R;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class UiToast {
Context mContext;
String str;
int id;

public UiToast(Context context,String string){
mContext = context;
str = string;
}

public UiToast(Context context,int id){
mContext = context;
str = context.getResources().getString(id);
}

public void show(){
Toast toast = new Toast(mContext);
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.view_toast, null);
TextView text = (TextView) contentView.findViewById(R.id.view_toast_textview);
if(!TextUtils.isEmpty(str)){
text.setText(str);
toast.setView(contentView);
toast.setGravity(Gravity.TOP, Gravity.LEFT, Gravity.TOP + 100);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}

}

public String getStr() {
return str;
}

public void setStr(String str) {
this.str = str;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: