一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久99深爱久久99精品_亚洲一区二区三区四区五区午夜_日本在线观看一区二区

Android 自定義Toast示例含代碼

Android 自定義Toast示例

您可以在Android中創(chuàng)建自定義Toast。愛掏網(wǎng) - it200.com因此,您可以在Toast上顯示一些圖像,如祝賀或失敗。愛掏網(wǎng) - it200.com這意味著您現(xiàn)在可以自定義Toast。愛掏網(wǎng) - it200.com

activity_main.xml

拖動(dòng)您想要在主Activity上顯示的組件。愛掏網(wǎng) - it200.com

<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.javatpoint.com.customtoast.MainActivity">  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Hello World!"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintLeft_toLeftOf="parent"  
        app:layout_constraintRight_toRightOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  

</android.support.constraint.ConstraintLayout>  

customtoast.xml

在layout目錄中創(chuàng)建另一個(gè)xml文件。愛掏網(wǎng) - it200.com在這個(gè)xml文件中,我們有一個(gè)ImageView和一個(gè)TextView。愛掏網(wǎng) - it200.com

<?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:id="@+id/custom_toast_layout"  
    android:orientation="vertical"  
    android:background="#F14E23"  
    >  

    <ImageView  
        android:id="@+id/custom_toast_image"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:contentDescription="Hello world"  
        android:src="https://deepinout.com/android/android-top-articles/@drawable/jtp_logo"/>  

    <TextView  
        android:id="@+id/custom_toast_message"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:contentDescription="To"  
        android:text="JavaTpoint custom Toast" />  
</LinearLayout>  

Activity類

現(xiàn)在編寫代碼來顯示自定義的toast。愛掏網(wǎng) - it200.com

package example.javatpoint.com.customtoast;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
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);

        //Creating the LayoutInflater instance
        LayoutInflater li = getLayoutInflater();
        //Getting the View object as defined in the customtoast.xml file
        View layout = li.inflate(R.layout.customtoast,(ViewGroup) findViewById(R.id.custom_toast_layout));

        //Creating the Toast object
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setView(layout);//setting the view of custom toast layout
        toast.show();
    }
}

輸出:

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。
發(fā)表評(píng)論
更多 網(wǎng)友評(píng)論0 條評(píng)論)
暫無評(píng)論

返回頂部

主站蜘蛛池模板: 欧美精品导航 | 亚洲日韩中文字幕一区 | 成人精品一区二区三区中文字幕 | 日韩一区在线观看视频 | 国产一区二区视频在线 | 国产精品178页 | 欧美日韩国产一区 | 91久久精 | 超碰人人做 | 国产日韩欧美一区 | 成人午夜视频在线观看 | 欧美日批 | 日本三级在线网站 | 欧美福利一区 | 精品亚洲一区二区 | 久久精品国产一区二区三区不卡 | 成人国产精品一级毛片视频毛片 | 夜夜草导航 | 国产日韩欧美一区二区 | 国产中的精品av涩差av | 亚洲精品一区国产精品 | 久久精品国产一区二区电影 | 日韩欧美国产一区二区三区 | 一区二区视频在线观看 | 亚洲在线 | 久久一区精品 | 国产激情一区二区三区 | 一区二区视频在线观看 | 黄色大片免费网站 | 欧美日韩高清免费 | 亚洲二区精品 | 欧美视频xxx | 网站一区二区三区 | 国产精品www | 亚洲精品一区二区三区 | 99亚洲精品 | 久久高潮 | 欧美黄页| 国产在线视频网 | 精品国产乱码久久久久久图片 | 久久久久久女 |