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

Android 自定義Toast示例含代碼

Android 自定義Toast示例

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

activity_main.xml

拖動您想要在主Activity上顯示的組件。愛掏網 - 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目錄中創建另一個xml文件。愛掏網 - it200.com在這個xml文件中,我們有一個ImageView和一個TextView。愛掏網 - 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類

現在編寫代碼來顯示自定義的toast。愛掏網 - 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();
    }
}

輸出:

聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。
發表評論
更多 網友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 国产成人网 | 欧美国产亚洲一区二区 | 五月婷婷丁香婷婷 | 色综合一区二区 | 国产男女猛烈无遮掩视频免费网站 | 久久av一区| 国产做a爱片久久毛片 | 国产精品视频一二三 | 在线观看中文字幕视频 | 亚洲精品一二三区 | 国产一区二区三区在线 | 99久久久久久 | av片免费| 亚洲天堂999 | 亚洲一区二区久久 | 激情a| 懂色av一区二区三区在线播放 | 在线视频中文字幕 | 在线一区二区三区 | 99视频免费在线观看 | 妞干网福利视频 | 草草在线观看 | 2020国产在线 | 欧美精品一区免费 | 中文字幕一级 | 久久99精品久久久久蜜桃tv | 亚洲视频一区在线观看 | 日韩欧美亚洲 | aⅴ色国产 欧美 | 久久久久久国产精品三区 | 一区亚洲 | 国产精品福利在线 | 亚洲一二视频 | 国产精品久久久久久久久久久久久久 | 欧美国产日韩一区二区三区 | 99精品热视频 | 日韩成人精品在线 | 久国久产久精永久网页 | 久久久久久国产精品 | 九色网址 | 久久久成人一区二区免费影院 |