• android自定义风格的toast


    先上图看一下我的自定义toast的样式


    源码下载地址:

    CustomToastActivity.java源码

    package com.jinhoward.ui_customtoast;
    /**
     * Author :jinhoward
     * Date:2013-07-21
     * Funtion: A toast of custom style.
     */
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class CustomToastActivity extends Activity {
    	private Button button;
    	
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_custom_toast);
    		
    		button=(Button)findViewById(R.id.button1);
            button.setOnClickListener(new View.OnClickListener() {
    			
    
    			public void onClick(View v) {
    				// TODO Auto-generated method stub
    				
    				LayoutInflater inflater = getLayoutInflater();
    				View customLayoutView = inflater.inflate(R.layout.toast_layout,
    				                               null);
    
    				Toast toast = new Toast(getApplicationContext());
    				
    				//设置toast为垂直居中显示
    				toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    				toast.setDuration(Toast.LENGTH_LONG);
    				toast.setView(customLayoutView);
    				toast.show();
    				
    			}
    		});
    
    }
    }
    
    

    activity_custom_toast.xml源码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:background="#D1EEEE">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_gravity="center"
            android:text="显示自定义的Toast" />
    
    </LinearLayout>

    toast_layout.xml源码:

    <RelativeLayout 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="#DAAAAA"
        android:orientation="horizontal"
        android:padding="8dp" >
    
        <ImageView
            android:id="@+id/ImageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/lock" />
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" 
            android:paddingLeft="10sp"
            android:gravity="top|left|center"
            android:layout_toRightOf="@+id/ImageView1"
            android:textStyle="bold"
            android:text="这是一个自定义的toast!"
            android:textColor="#FF0000" />
    
    </RelativeLayout>


  • 相关阅读:
    技术普及帖:你刚才在淘宝上买了一件东西
    centos 安装yum
    eclipse 中java 项目红色感叹号终极解决方案
    centos x8664位版本 想安装qq for linux
    甲骨文放弃对RHEL的ASMLib支持
    amoeba连接mysqlERROR 2006 (HY000): MySQL server has gone away
    让Fedora 15/CentOS 6显示详细启动信息,不显示进度条
    Oracle10g RAC安装手册
    如何控制oracle RAC 进行并行运算
    利用mysql的inet_aton()和inet_ntoa()函数存储IP地址
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3206440.html
Copyright © 2020-2023  润新知