Android Toast的多种样式

来源:互联网 发布:猎马搜索源码 编辑:程序博客网 时间:2024/05/22 08:12

Toast是安卓开发中经常要使用的与用户的交互工具,除了系统默认的样式以外,其实我们也可以自己定义其样式,让我们的程序交互变得更加人性化。
首先在主布局文件中添加两个按钮,分别用来点击后显示Toast的默认样式与自定义样式,然后在MainActivity类中声明并初始化按钮。

<LinearLayout 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:orientation="vertical"    tools:context="com.example.toasttest.MainActivity" >    <Button        android:id="@+id/show_default_toast"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示默认的Toast" />    <Button        android:id="@+id/show_coustom_toast"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="显示自定义的Toast" /></LinearLayout>
    //默认样式    private Button default_toast;    //自定义样式    private Button custom_toast;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        default_toast = (Button) findViewById(R.id.show_default_toast);        custom_toast = (Button) findViewById(R.id.show_coustom_toast);        default_toast.setOnClickListener(this);        custom_toast.setOnClickListener(this);    }

MainActivity要implements OnClickListener
实现onClick()方法,首先实现点击default_toast按钮显示默认Toast的功能。

public void onClick(View v) {        switch (v.getId()) {        case R.id.show_default_toast:            Toast.makeText(MainActivity.this, "默认的Toast", Toast.LENGTH_SHORT).show();            break;        }}

运行结果如下所示

Toast的默认样式较为简单,往往不能满足我们的需求,我们需要为设定自定义样式。
其实,Toast类当中有一个setView()方法,可以为Toast设置一个自定义视图,并为其设定显示位置。在onClick()方法中增添如下代码:

case R.id.show_coustom_toast:            LinearLayout linear = new LinearLayout(MainActivity.this);            linear.setOrientation(LinearLayout.VERTICAL);            TextView text = new TextView(MainActivity.this);            text.setText("图片Toast");            ImageView image = new ImageView(MainActivity.this);        image.setImageResource(R.drawable.ic_launcher);            linear.addView(text);            linear.addView(image);            Toast image_toast = Toast.makeText(MainActivity.this, "自定义的Toast", Toast.LENGTH_SHORT);            image_toast.setView(linear);            image_toast.setGravity(Gravity.CENTER, 100, -10);            image_toast.show();break;

程序中,首先声明了一个LinearLayout布局,并为其添加了一个TextView组件和一个ImageView组件。并定义了image_toast的显示位置为居中向右偏一百像素,向上偏十像素。
显示效果如下:

要注意的是,当我们为Toast设定view后,其makeText(MainActivity.this, “自定义的Toast”, Toast.LENGTH_SHORT);中设定的提示消息就会无效。

此外,我们还可以来设定更加详细的布局效果。
在drawable文件夹下新建一个toast_back.xml文件,将其作为Toast布局文件的背景

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 四个角度的弧度 -->    <corners android:radius="1dp" />    <!-- 背景颜色渐变效果 -->    <gradient        android:endColor="#5FC1F3"        android:startColor="#C39142" />    <!-- 间距 -->    <padding        android:bottom="25dp"        android:left="10dp"        android:right="5dp"        android:top="10dp" /></shape>
linear.setBackgroundResource(R.drawable.toast_back);

通过xml文件,改变了Toast的背景颜色、弧度以及间距等,自定义Toast的显示效果如下:

以上布局也可以通过xml文件来定义,这样可以让代码看起来更加的简洁。
在Layout文件夹下新建一个toast_layout.xml,同样是设定一个TextView和一个ImageView,代码如下:

<?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:background="@drawable/toast_back"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:gravity="center"        android:text="这是一个默认的Toast" />    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:src="@drawable/ic_launcher" /></LinearLayout>

更改onClick()方法

case R.id.show_coustom_toast:    LayoutInflater inflater = getLayoutInflater();    LinearLayout linear = (LinearLayout)     inflater.inflate(R.layout.toast_layout, null);    Toast toast = new Toast(MainActivity.this);    toast.setView(linear);    toast.setDuration(Toast.LENGTH_SHORT);    toast.setGravity(Gravity.CENTER, 0, 0);    toast.show();    break;

最后呈现的效果是一样的。

此外,也可以将根布局的背景设为一张.9.png图片,这样可以获得很好的显示效果。
为drawable文件夹引入如下图片

改变toast_layout.xml文件中根布局的背景

android:background="@drawable/right"

显示效果如下:

可以看出来,因为使用的图片是9.png格式的文件,所以图片的圆滑形状也不会因为包裹的内容的多少而变化。

工程代码下载地址:Toast的多种样式

0 0
原创粉丝点击