s
下面分别采用两种方式创建Toast(转:http://zhoujianghai.iteye.com/blog/814940)
/**
*自定义美化Toast,使用图片作背景
*/
private void showToast() {
Toast t = new Toast(this);
t.setDuration(Toast.LENGTH_SHORT);
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundResource(R.drawable.toast_bg);
TextView textView = new TextView(this);
textView.setText("javaeye:您好!");
textView.setTextSize(14);
textView.setTextColor(Color.WHITE);
layout.addView(textView);
t.setView(layout);
t.setGravity(Gravity.TOP, 100+20, 200 + 20);
t.show();
}
/**
* 默认的Toast效果
*/
private void showToast2() {
Toast t = Toast.makeText(this, "csdn:您好!", Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP, 100+20, 200 + 20);
t.show();
}
===================
通过json动态创建 toast
如下所示:
可以通过testCreateJsonObj()创建测试json,也可以直接通过json字符串创建。
Toast toast = Toast.makeText(MainActivity.this, "", Toast.LENGTH_LONG);
//View textView = toast.getView();
LinearLayout lay = new LinearLayout(MainActivity.this);
lay.setBackgroundResource(R.drawable.toast_bg);
lay.setOrientation(LinearLayout.VERTICAL);
//add img
ImageView view = new ImageView(MainActivity.this);
view.setImageResource(R.drawable.ic_launcher);
lay.addView(view);
//JSONObject data = testCreateJsonObj();
JSONObject data = null;
try {
data = new JSONObject("{\"array\":[{\"color\":\"#ffff0000\",\"text\":\"andytest1\",\"size\":\"24\",\"gravity\":\"center\"},{\"color\":\"#ff0000ff\",\"text\":\"andytest2\n换行测试\",\"size\":\"14\",\"gravity\":\"left\"}],\"layout\":{\"gravity\":\"center\"}}");
} catch (JSONException e1) {
e1.printStackTrace();
}
Log.e("andy_show_json:", data.toString());
int pos = Gravity.CENTER;
try {
pos = str2pos(data.getJSONObject("layout").getString("gravity"));
JSONArray array = data.getJSONArray("array");
for (int i = 0; i < array.length(); i++) {
JSONObject iObj = array.getJSONObject(i);
addTextView(lay, iObj);
}
} catch (JSONException e) {// 抛错 说明JSON字符不是数组或根本就不是JSON
e.printStackTrace();
}
toast.setGravity(pos, 0, 0);
toast.setView(lay);
toast.show();相关函数:
public JSONObject createJsonObject(HashMap<String, String> hashMap) {
JSONObject jsonobj = new JSONObject();
Set<String> keys = hashMap.keySet();
try {
for (Iterator<String> iterator = keys.iterator(); iterator
.hasNext();) {
String key = iterator.next();
jsonobj.put(key, hashMap.get(key));
}
} catch (JSONException e) {
e.printStackTrace();
}
return jsonobj;
}
public JSONArray createJsonArray(ArrayList<HashMap<String, String>> mylist) {
JSONArray array = new JSONArray();
for (Iterator<HashMap<String, String>> iterator = mylist.iterator(); iterator
.hasNext();) {
JSONObject jsonobj = createJsonObject(iterator.next());
array.put(jsonobj);
}
return array;
}
public JSONObject testCreateJsonObj(){
JSONObject jsonobj=new JSONObject();
try {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("text", "andytest1");
map.put("size", "24");
map.put("color", "#ffff0000");
map.put("gravity", "center");
mylist.add(map);
map = new HashMap<String, String>();
map.put("text", "andytest2\n换行测试");
map.put("size", "14");
map.put("color", "#ff0000ff");
map.put("gravity", "left");
mylist.add(map);
map = new HashMap<String, String>();
map.put("gravity", "center");
jsonobj.put("layout", createJsonObject(map));
jsonobj.put("array", createJsonArray(mylist));
} catch (JSONException e) {
e.printStackTrace();
}
return jsonobj;
}
private int str2pos(String str) {
int pos = Gravity.CENTER;
if(str.equals("right")) {
pos = Gravity.RIGHT;
} else if(str.equals("left")){
pos = Gravity.LEFT;
} else {//center
pos = Gravity.CENTER;
}
return pos;
}
private int color2int(String color) {
// 服务器端下发color格式:#ffcc00cc 或 #ffcc00
int value = 0xff000000;
if (color != null && color.startsWith("#")
&& (color.length() == 7 || color.length() == 9)) {
try {
value = Color.parseColor(color);
} catch (Exception e) {
e.printStackTrace();
}
}
return value;
}
private void addTextView(LinearLayout lay, JSONObject iObj) {
TextView textView = new TextView(MainActivity.this);
try {
textView.setText((String)iObj.get("text"));
textView.setTextSize(Integer.valueOf((String)iObj.get("size")));
textView.setTextColor(color2int((String)iObj.get("color")));
textView.setGravity(str2pos((String)iObj.get("gravity")));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lay.addView(textView, p);
}
s
s

241

被折叠的 条评论
为什么被折叠?



