Android学习之Toast的自定义_标题栏的隐藏

Toast.makeText(context,text, duration).show();是系统默认的Toast其实在开发中根据我们需要的不同,我们还可以对Toast进行自定义

 

自定义位置的Toast:

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.show()

自定义视图的Toast:

新建布局文件toast_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="8dp"
              android:background="#DAAA"
              >
    <ImageView android:src="@drawable/droid"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginRight="8dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              />
</LinearLayout>

在点击事件的处理方法中书写代码:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
                               (ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

即可完成一个自定义视图的Toast。

 

到这里我们学习了两种自定义的Toast一个是位置,一个是视图,通过查看源代码我们知道Toast显示时间的设置只有Toast.LENGTH_SHORT和Toast.LENGTH_LONG两个值,当这两个值所设置的显示时间不满足我们的需求的时候,我们就需要自定义Toast的显示时间。

在源码中我们看到,show方法是控制Toast显示的,另外还有一个方法是cancel是取消Toast显示的。如果我们能在一定的时间段后调用Toast的cancel方法就可以控制Toast的显示时间。在Android中有这样一个东西就是Handler翻译成中文叫做操作者,而操作者,是用来操作出来消息的,消息在传递的过程中,是可以指定它的延迟时间的,我们可以通过Handler的消息延迟操作来控制,Toast的show和cancel的调用。来达到控制Toast显示时间的目的。

示例代码如下:

自定义Toast,将显示和取消两个方法分开写:

import android.content.Context;

import android.widget.Toast;

 

public class MyToast {

        

         privateContext context;

         privateToast toast;

         publicMyToast(Context context){

                   this.context=context;

         }

         public  void show(String str){

                   if(toast==null){

                            toast=Toast.makeText(context,str, 0);

                            toast.setDuration(Toast.LENGTH_LONG);

                            toast.show();

                   }else{

                            toast.setText(str);

                            toast.show();

                   }

         }

         publicvoid cancel(){

                   if(toast!=null){

                            toast.cancel();

                   }

         }

}

在Activity的代码:

 

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.app.Activity;

import android.view.View;

import android.widget.Button;

public class MainActivity extends Activity{

         Buttontext;

         MyToastmytoast;

         Handlerhandler=new Handler(){

                   publicvoid handleMessage(android.os.Message msg) {

                            mytoast.cancel();

                   };

         };

         @Override

         protectedvoid onCreate(Bundle savedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.btn);

                   //找到文本控件

                   text=(Button)findViewById(R.id.btn);

         }

         publicvoid btnclick(View v){

                   mytoast=newMyToast(this);

                   mytoast.show("我是自定义时间的Toast");

                   Messagemsg=new Message();

                   handler.sendMessageDelayed(msg,200);

         }

}

 

到这里全部的自定义Toast都,讲解完毕。但是到目前为止,每次我们运行我们所写的应用,是不是顶部都有一个黑色的标题栏啊,而我们正常的Android手机上的应用是没有这块黑色区域的,其实想让这块黑色区域消失,只需要一行代码,就是requestWindowFeature(Window.FEATURE_NO_TITLE);将这行代码写在setsetContentView();前即可;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值