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 ...
随机推荐
- Jenkin+TestNG进行自动化测试执行
1.登陆jenkins'后,主页面有一个jenkins管理选项,进入该模块,对插件,系统进行配置.(安装一个extend choice parameter插件) 2.点击new item新建一个项目, ...
- QTP检查点和参数化_百度一下
一.脚本编辑检查点: Browser("百度知道 - 全球最大中文互动问答平台").Page("百度知道 - 全球最大中文互动问答平台").WebEdit(&q ...
- Convert to Objective-C ARC
今天在进行代码走查时,竟然发现了下面这段代码: Bad Code 顿时感觉吐槽无力,虽然我反复强调内存管理问题,无非就是谁申请谁释放,利用强弱引用避免 retain-cycles,但是还是会有这样那样 ...
- Struts2 Spring Hibernate Ajax Java总结(实时更新)
1. 在form表单的onload属性里的方法无法执行? 若忘记了在<%=request.getSession().getAttribute("userName")%> ...
- .net之单元测试
一.单元测试的目的是为了提高项目的质量 二..net单元测试的实施步骤是:在已经创建的类文件的某个方法上右键-->选择创建单元测试-->系统会创建单元测试解决方案--->单元测试解决 ...
- bzoj 1037 [ZJOI2008]生日聚会Party(DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1037 [题意] 一排n男m女,求满足任意连续段男女人数之差不超过k的数目. [思路] ...
- Python【基础第一篇】
一.Python3新特性 编码统一为unicode Python3不支持Twisted,暂时只支持73% 1/2=0.5 print "hello World" 变成 print ...
- ubuntu 常用软件配置
1. 首先重装系统后需要执行: sudo apt-get install update 2. 然后安装必要的软件: terminator, vim, git,
- HDU-3436 Queue-jumpers 树状数组 | Splay tree删除,移动
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3436 树状数组做法<猛戳> Splay tree的经典题目,有删除和移动操作.首先要离散化 ...
- [转] C++ Redistributable Package版本详解
我们使用的程序常常都需要C++ Redistributable Package的支持.C++ Redistributable Package有众多版本,给安装带了不便. 目前(2013-12-04) ...