码迷,mamicode.com
首页 > 其他好文 > 详细

User Interface 之自定义Toast

时间:2016-07-06 23:17:47      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

通过Toast的setView(View view)方法可以将Toast的内容设为任意视图

eg:

toast_layout.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/toast_layout_root"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cover"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="I am a custom toast"
        android:textSize="20sp"
        android:typeface="monospace"/>
</LinearLayout>

 

  

 

MainActivity.java

package com.panjn.userinterface;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); View toastView = getLayoutInflater().inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.toast_layout_root)); Toast toast = new Toast(getApplicationContext()); // toast.setText("Hello Minions"); //不能调用setText(String text), 否则抛出异常 toast.setView(toastView); toast.setDuration(Toast.LENGTH_LONG); //params: int gravity, int xOffset, int yOffset toast.setGravity(Gravity.CENTER,0,0); toast.show(); } }

  

User Interface 之自定义Toast

标签:

原文地址:http://www.cnblogs.com/mmyz-sysu-panjn/p/toast.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!