Android简易实战教程--第三十一话《自定义土司》
最近有点忙,好几天不更新博客了。今天就简单点,完成自定义土司。
主布局文件代码:
<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" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="显示普通的通知" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp"
android:text="自定义通知" /> </RelativeLayout>
一个用于显示原生土司,一个用于显示自定义土司。
自定义土司的布局:一张图片,一段文本。都是自定义上去
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#44ff4400"
android:orientation="horizontal"
android:padding="10dp" > <ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp" /> <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF" /> </LinearLayout>
主活动代码:
package com.example.android_toast2; import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private Button button;
private Button button2; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(MainActivity.this, "提示信息",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}); button2 = (Button) this.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = new Toast(MainActivity.this);
// 加载自定义布局
View view = LayoutInflater.from(MainActivity.this).inflate(
R.layout.dialog, null);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
imageView.setImageResource(R.drawable.a);
TextView textView = (TextView) view.findViewById(R.id.text);
textView.setText("自定义的吐司通知");
toast.setDuration(Toast.LENGTH_LONG);//显示时长
toast.setGravity(Gravity.CENTER, 0, 0);//土司位于父布局的位置
// 加载自定义的布局
toast.setView(view);
toast.show();
//统一UI风格,需要自定义的通知,声明在一个类中,可以反复调用 }
});
}
}
运行看看效果:
Android简易实战教程--第三十一话《自定义土司》的更多相关文章
- Android简易实战教程--第五十一话《使用Handler实现增加、减少、暂停计数》
转载博客请注明出处:道龙的博客 之前,写过一篇使用异步任务AysncTask实现倒计时的小案例,喜欢的话可以参考博客:Android简易实战教程--第三十三话< AsyncTask异步倒计时&g ...
- Android简易实战教程--第三十八话《自定义通知NotifiCation》
上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动.但是那里的通知没有加入些"靓点",这一篇就给它加入自定义的布局,完成自定义的通知. 应用:比如QQ音乐为例,当点击音乐播 ...
- Android简易实战教程--第三十六话《电话录音》
今天完成一个简单的电话录音功能,即接通电话后,立即录下自己打电话的声音.实现起来比较简单:一个服务,一个TelephonyManager.一个MediaRecorder就够了. 1.布局提供一个开启录 ...
- Android简易实战教程--第三十五话《音乐播放》
已经好几天不更新博客了,今天轻松一点模拟个简单的"音乐播放器".1分钟看完~ 整个简单布局,加几个控制按钮: <LinearLayout xmlns:android=&quo ...
- Android简易实战教程--第三十二话《使用Lrucache和NetworkImageView加载图片》
转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:小杨的博客 http://blog.csdn.net/qq_32059827/article/details/5279131 ...
- Android简易实战教程--第三十话《撕衣美女》
此篇邪恶一些,给单身屌丝发点"福利",通过图片的绘制,给美女脱掉衣服. 原理:图片覆盖图片,通过画笔对顶端的图片做一些特效处理,即手指触摸的地方,设置为透明.即可显示最底部的美女图 ...
- Android简易实战教程--第三十九话《Chronometer实现倒计时》
Android提供了实现按照秒计时的API,今天就是用这个API实现简单的倒计时. 来个布局: <?xml version="1.0" encoding="utf- ...
- Android简易实战教程--第三十九话《简单的模糊查询》
今天这一篇小案例模拟模糊查询,即输入一个字符,显示手机对应的所有存在该字符的路径. 布局: <?xml version="1.0" encoding="utf-8& ...
- Android简易实战教程--第三十四话《 自定义SeekBar以及里面的一些小知识》
转载本专栏文章,请注明出处尊重原创:博客地址http://blog.csdn.net/qq_32059827/article/details/52849676:小杨的博客 许多应用可能需要加入进度,例 ...
随机推荐
- 实战分享:如何成功防护1.2T国内已知最大流量DDoS攻击
作者:腾讯云宙斯盾安全团队&腾讯安全平台部 引言: DDoS攻击势头愈演愈烈,除了攻击手法的多样化发展之外,最直接的还是攻击流量的成倍增长.3月份国内的最大规模DDoS攻击纪录还停留在数百G规 ...
- spring初学总结思路
spring初步学习总结 总概括有四个方面:IOC,AOP,JDBC,和事务管理 ioc:实现了new类的一个权力的反转.(平时new类导致代码冗余,手动new类无法满足动态new类的需求) aop: ...
- 机器学习技法:10 Random Forest
Roadmap Random Forest Algorithm Out-Of-Bag Estimate Feature Selection Random Forest in Action Summar ...
- vue插件移动框
npm i dragablemodel -S(安装插件) import dragablemodel from 'dragablemodel' Vue.use(loading) 模板(组件) <d ...
- [BZOJ]2589: Spoj 10707 Count on a tree II
Time Limit: 20 Sec Memory Limit: 400 MB Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v),你需要回答u xor last ...
- 【USACO】又买饲料 单调队列dp
题目描述 约翰开车回家,又准备顺路买点饲料了(咦?为啥要说“又”字?)回家的路程一共有 E 公里, 这一路上会经过 N 家商店,第 i 家店里有 F i 吨饲料,售价为每吨 C i 元.约翰打算买 K ...
- hihocoder1258(水)(2015ACM/ICPC北京站)
题意: 给你B,C,S三种模式,当出现S时直接得分最多300(即perfect) 当是B,C时后面会跟一个数字,当后面的数字是从1开始的连续时,直接得分最多300(即perfect) 问给你一系列,最 ...
- Unity脚本自动添加注释脚本及排版格式
Unity脚本自动添加注释脚本及头部注释排版格式 公司开发项目,需要声明版权所有,,,,标注公司名,作者,时间,项目名称及描述等等. 自己总结实现的现成脚本及头部注释排版文本,添加到模版即可. 文件不 ...
- jquery easyui datagrid设置行样式 不可删除某行
rowStyler: function (index,row) { if (parseInt(row.ksrs) > 0) { return 'color:red'; } }, onLoadSu ...
- localeCompare() 方法实现中文的拼音排序
google了很多次才发现在国外网站上有提示如何比较中文,原文地址:http://www.webdeveloper.com/forum/showthread.php?t=9365 前提:使用Unico ...