Android_Toast
xml文件:
main1:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.toastdemo.MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示带有图片的toast"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义的Toast"
android:textSize="20sp"/> </LinearLayout> </RelativeLayout>
main2.xml
<?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" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义内容"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/> </LinearLayout>
源代码:
package com.example.toastdemo; import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast; public class MainActivity extends Activity { private Button btn_1;
private Button btn_2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2); /**
* 显示带有图片的Toast
*/
btn_1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(MainActivity.this, "show Toast with the picture", Toast.LENGTH_SHORT);
LinearLayout toastLayout = (LinearLayout) toast.getView();
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
toastLayout.addView(image,0);//设置图片于文字上
//设置toast显示的位置,其中X方向偏移量左-右+,y方向的偏移量上-下+
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
/**
* 显示自定义的Toast
*/
btn_2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = new Toast(MainActivity.this);
LayoutInflater layout_inflate = LayoutInflater.from(MainActivity.this);
View inflater = layout_inflate.inflate(R.layout.main2, null);
//设置自定义的视图,并展示
toast.setView(inflater);
toast.show();
}
});
}
}
Android_Toast的更多相关文章
- android 开发-Toast控件的实现
Toast吐司: Toast内容简单,不做过多介绍,Toast支持自带简单吐司,自定义吐司.内容简单可见代码,详见API.A toast provides simple feedback about ...
随机推荐
- java问题若干
1.Java处理本身包含双引号的String 解决:使用转义字符.如:String str = "select * from \"TAB_catalog\" " ...
- POJ 3377 Ferry Lanes
虽然它出现在dp专场里···但是我第一反应是一道最短路题···不过幸好它出现在dp专场里···因为我不怎么会dijstra什么的··· 题意:一条河上有N+1对码头,每个相邻码头之间需要一定时间到达, ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.8
Prove that for any matrices $A,B$ we have $$\bex |\per (AB)|^2\leq \per (AA^*)\cdot \per (B^*B). \ee ...
- 设计模式之Memento(备忘机制)
Memento备望录模式定义:memento是一个保存另外一个对象内部状态拷贝的对象.这样以后就可以将该对象恢复到原先保存的状态. Memento模式相对也比较好理解,我们看下列代码: public ...
- web.xml 配置的详解
http://my.oschina.net/u/1383439/blog/224448 http://blog.csdn.net/guihaijinfen/article/details/836383 ...
- as3+java+mysql(mybatis) 数据自动工具(五)
现在介绍常量的配置,主要用于客户端(as3)与服务端(java)之间的常量同步,比如错误码.请求标识等 配置格式: <macros name="Macros" groupSt ...
- leetcode—Palindrome 解题报告
1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...
- leetcode@ [174] Dungeon Game (Dynamic Programming)
https://leetcode.com/problems/dungeon-game/ The demons had captured the princess (P) and imprisoned ...
- [iOS基础控件 - 4.5] 猜图游戏
A.需要掌握的 1.添加图片资源(暂时认为@2x跟非@2x代表同一张图片) 2.搭建UI界面* 文本标签* 4个按钮* 中间的图片 3.设置状态栏样式 4.监听下一题按钮的点击 5.延迟加载数据* 加 ...
- C#操作JSON学习
JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的文本格式,可以很容易在 ...