• 自定义Toast的显示位置和显示内容


     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:orientation="vertical"
     8     tools:context="com.example.leeson7_1_id19.MainActivity">
     9 
    10     <Button
    11         android:onClick="toast_1"
    12         android:layout_width="match_parent"
    13         android:layout_height="wrap_content"
    14         android:text="普通的toast" />
    15     <Button
    16         android:onClick="toast_2"
    17         android:layout_width="match_parent"
    18         android:layout_height="wrap_content"
    19         android:text="自定义的toast" />
    20     <Button
    21         android:onClick="toast_3"
    22         android:layout_width="match_parent"
    23         android:layout_height="wrap_content"
    24         android:text="自定义位置的toast" />
    25 </LinearLayout>
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical">
     6 
     7     <TextView
     8         android:layout_width="match_parent"
     9         android:layout_height="wrap_content"
    10         android:text="自定义位置文本"
    11         android:gravity="center"
    12         android:background="#ff0000"
    13         android:padding="5dp"
    14         android:textSize="18sp"/>
    15 
    16 
    17 </LinearLayout>
     1 package com.example.leeson7_1_id19;
     2 
     3 import android.os.Bundle;
     4 import android.support.v7.app.AppCompatActivity;
     5 import android.view.Gravity;
     6 import android.view.View;
     7 import android.widget.ImageView;
     8 import android.widget.Toast;
     9 
    10 public class MainActivity extends AppCompatActivity {
    11 
    12     @Override
    13     protected void onCreate(Bundle savedInstanceState) {
    14         super.onCreate(savedInstanceState);
    15         setContentView(R.layout.activity_main);
    16     }
    17 
    18     public void toast_1(View V){
    19         // 设置土司显示的内容和时长并显示出来
    20         Toast.makeText(this,"我是一个普通的toast",Toast.LENGTH_SHORT).show();
    21     }
    22     public void toast_2(View V){
    23         // 自定义土司
    24         // 创建土司
    25         Toast toast = new Toast(this);
    26         // 设置土司显示的时间长短
    27         toast.setDuration(Toast.LENGTH_SHORT);
    28         // 创建ImageView
    29         ImageView img = new ImageView(this);
    30         // 设置图片的资源路径
    31         img.setImageResource(R.mipmap.ic_launcher);
    32         // 设置土司的视图图片
    33         toast.setView(img);
    34         // 显示土司
    35         toast.show();
    36     }
    37     public void toast_3(View V){
    38         // 自定义土司显示位置
    39         // 创建土司
    40         Toast toast = new Toast(this);
    41         // 找到toast布局的位置
    42         View layout = View.inflate(this,R.layout.toast,null);
    43         // 设置toast文本,把设置好的布局传进来
    44         toast.setView(layout);
    45         // 设置土司显示在屏幕的位置
    46         toast.setGravity(Gravity.FILL_HORIZONTAL|Gravity.TOP,0,70);
    47         // 显示土司
    48         toast.show();
    49     }
    50 }

     

  • 相关阅读:
    Nginx负载均衡配置
    云鹏在美团八年工作总结
    尽量不要让儿女从事这3种工作,钱再多也别做,坚持再久也没前途
    Nginx 配置ip_hash
    postgresql获取表结构,表名、表注释、字段名、字段类型及长度和字段注释
    更快的Maven来了,我的天,速度提升了8倍!
    Nginx负载均衡配置及算法详解
    nginx负载均衡策略:ip_hash、url_hash
    Windows Phone实用开发技巧(41):解决WebBrowser中显示黑色背景网页闪屏
    Windows Phone实用开发技巧(32):照片角度处理
  • 原文地址:https://www.cnblogs.com/lxjhoney/p/6517769.html
Copyright © 2020-2023  润新知