Android-Toast的属性设置
来源:互联网 发布:playclub人物数据 编辑:程序博客网 时间:2024/05/14 16:04
目标效果:
第一个图为所有的Button控件,从上到下点击分别显示不同的Toast效果。
1.activity_main.xml页面定义Button控件。
activity_main.xml页面:
<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=".MainActivity" > <Button android:id="@+id/btnChange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="114dp" android:text="改变位置Toast" /> <Button android:id="@+id/btnNew" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnImage" android:layout_centerHorizontal="true" android:layout_marginTop="19dp" android:text="自定义Toast" /> <Button android:id="@+id/btnToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="48dp" android:text="默认Toast" /> <Button android:id="@+id/btnImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btnToast" android:layout_below="@+id/btnChange" android:layout_marginTop="15dp" android:text="图片Toast" /></RelativeLayout>
2.layout文件夹中定义toast.xml页面,作为最后一自定义Toast的布局页面。
toast.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/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="自定义1" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="自定义2" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout></LinearLayout>
3.MainActivity.java页面处理点击事件。
MainActivity.java页面:
package com.example.toast;import java.util.zip.Inflater;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.view.Gravity;import android.view.LayoutInflater;import android.view.Menu;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 implements OnClickListener{private Button btnToast,btnChange,btnImage,btnNew;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnToast=(Button) findViewById(R.id.btnToast);btnChange=(Button) findViewById(R.id.btnChange);btnImage=(Button) findViewById(R.id.btnImage);btnNew=(Button) findViewById(R.id.btnNew);btnToast.setOnClickListener(this);btnChange.setOnClickListener(this);btnImage.setOnClickListener(this);btnNew.setOnClickListener(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.btnToast:Toast toast1=Toast.makeText(this,"这是一个默认Toast",Toast.LENGTH_SHORT);toast1.show();break;case R.id.btnChange:Toast toast2=Toast.makeText(this,"这是一个改变位置的Toast",Toast.LENGTH_SHORT);toast2.setGravity(Gravity.CENTER,0,0);//第二个参数为X的偏移量:正值往右偏移,负值往左偏移;第三个参数为Y的偏移量:正值往下偏移,负值往上偏移toast2.show();break;case R.id.btnImage:Toast toast3=Toast.makeText(this,"这是一个图片Toast",Toast.LENGTH_SHORT);LinearLayout toast_layout=(LinearLayout) toast3.getView();//将Toast作为一个布局ImageView image=new ImageView(this);image.setImageResource(R.drawable.ic_launcher); //设置图片toast_layout.addView(image,0); //将图片添加到Toast上,0代表位置toast3.show();break;case R.id.btnNew:LayoutInflater inflater=LayoutInflater.from(this);View toast_view=inflater.inflate(R.layout.toast, null);Toast toast4=Toast.makeText(this,"这是一个默认Toast",Toast.LENGTH_SHORT);toast4.setView(toast_view); //将布局添加到Toast上toast4.show();break;default:break;}}}
4.运行就可以显示目标效果了。
0 0
- Android-Toast的属性设置
- Android 设置Toast的位置
- android toast设置比Toast.LENGTH_SHORT还短的持续时间
- android toast设置比Toast.LENGTH_SHORT还短的持续时间
- android toast设置比Toast.LENGTH_SHORT还短的持续时间
- Android 设置 Toast 的显示位置
- Android 设置 Toast 的显示位置
- android中如何设置Toast 的样式
- Android toast 设置
- android设置Toast样式
- android Toast设置
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- Android Toast 设置到屏幕中间以及其他自定义Toast的实现方法
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
- 训练2 23题
- 敏感词过滤
- JBoss4.0.0部署
- 从上往下打印二叉树
- PHP简介
- Android-Toast的属性设置
- BaseAdapter和Viewholder的抽取(模版设计模式)
- csdn markdown格式的一些记录
- Native与H5交互的那些事
- LeetCode-338. Counting Bits
- 错误:AAPT err(Facade for ): libpng error: Not a PNG file
- 山科ACM比赛总结和感悟
- 双臂14轴机器人YuMi
- 专题二总结 搜索算法