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 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: 亚洲视频在线一区二区三区| 天天操狠狠操夜夜操| 国产又粗又长又硬免费视频| 亚洲人成77777在线播放网站| 5x社区精品视频在线播放18| 欧美日韩国产高清一区二区三区| 在线观看中文字幕码2023| 亚洲美免无码中文字幕在线| 999精品视频在线观看| 波多野结衣免费在线观看| 夜夜添狠狠添高潮出水| 亚洲综合色婷婷| 720lu国内自拍视频在线| 欧美国产综合视频| 国产深夜福利在线观看网站| 五月天婷婷丁香| 青青草成人影视| 无码av大香线蕉伊人久久| 又黄又爽一线毛片免费观看| 一本到中文字幕高清不卡在线| 男女过程很爽的视频网站| 美女扒开超粉嫩的尿口视频| 欧美成人全部免费观看1314色| 国产色婷婷精品免费视频| 又爽又黄又无遮挡网站| 一本久久A久久免费精品不卡| 第四色亚洲色图| 国农村精品国产自线拍| 免费午夜爽爽爽WWW视频十八禁| eva樱花动漫网| 欧美黑人xxxx性高清版| 国产精品99久久久久久www| 久久男人av资源网站| 老师好大好爽办公室视频| 好爽好多水好得真紧| 亚洲欧美国产精品久久久| 国产视频你懂的| 成人片黄网站色大片免费观看app| 免费毛片a线观看| 2019亚洲午夜无码天堂| 日韩在线播放中文字幕|