Computer >> Máy Tính >  >> Lập trình >> Android

làm cách nào để thiết kế tin nhắn bánh mì nướng tùy chỉnh trong Android?

Trước khi tìm hiểu về Bánh mì nướng tùy chỉnh, chúng ta nên biết về bánh mì nướng là gì. Bánh mì nướng được sử dụng để hiển thị thông báo trên màn hình hiện tại đôi khi. Sau một thời gian, nó sẽ biến mất. Trong ví dụ này, chúng ta có thể tìm hiểu cách tùy chỉnh thông báo bánh mì nướng.

Ví dụ này minh họa về cách tạo tin nhắn bánh mì nướng tùy chỉnh trong android.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
   xmlns:android = "https://schemas.android.com/apk/res/android" xmlns:tools =    "https://schemas.android.com/tools" android:layout_width = "match_parent"
   android:layout_height = "match_parent">
   <LinearLayout
      android:layout_width = "match_parent"
      android:layout_height = "match_parent"
      android:background = "#797979"
      android:gravity = "center"
      android:orientation = "vertical">
      <Button
         android:id = "@+id/showToast"
         android:text = "Show Toast"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />
   </LinearLayout>
</android.support.constraint.ConstraintLayout>

Bước 3 - Thêm mã sau vào src / MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button button = findViewById(R.id.showToast);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            LayoutInflater li = getLayoutInflater();
            View layout = li.inflate(R.layout.custom_toast, (ViewGroup)       findViewById(R.id.custom_toast_layout_id));
            Toast toast = new Toast(getApplicationContext());
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);//setting the view of custom toast layout
            toast.show();
         }
      });
   }
}

Bước 4 - Bây giờ tạo bố cục bánh mì nướng tùy chỉnh trong res / layout / custom_toast.xml và thêm mã sau

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   android:id = "@+id/custom_toast_layout_id"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:gravity = "center">
   <LinearLayout
      android:layout_width = "wrap_content"
      android:layout_height = "62dp"
      android:gravity = "center"
      android:background = "@drawable/buttonshape"
      android:orientation = "horizontal">
      <ImageView
         android:id = "@+id/imageView"
         android:layout_width = "100dp"
         android:layout_height = "50dp"
         android:scaleType = "fitStart"
         android:src = "@drawable/logo" />
      <TextView
         android:id = "@id/text"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_marginLeft = "10dp"
         android:layout_marginRight = "20dp"
         android:text = "This is custom toast"
         android:textColor = "#FFF"
         android:textSize = "15sp"
         android:textStyle = "bold" />
   </LinearLayout>
</LinearLayout>

Bước 5 - Trong đoạn mã trên, chúng tôi đã thêm nền cho bố cục dưới dạng buttonhape trong drawable, vì vậy hãy tạo tệp xml trong drawable dưới dạng buttonhape.xml và thêm mã sau

<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "https://schemas.android.com/apk/res/android" android:shape = "rectangle" >
   <corners
      android:radius = "14dp"
   />
   <gradient
      android:angle = "45"
      android:centerX = "%"
      android:centerColor = "#47A891"
      android:startColor = "#E8E8E8"
      android:endColor = "#000000"
      android:type = "linear"/>
   <padding
      android:left = "0dp"
      android:top = "0dp"
      android:right = "0dp"
      android:bottom = "0dp"/>
   <size
      android:width = "270dp"
      android:height = "60dp"/>
   <stroke
      android:width = "3dp"
      android:color = "#878787"/>
   </shape>

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn

làm cách nào để thiết kế tin nhắn bánh mì nướng tùy chỉnh trong Android?

Bây giờ nhấp vào nút Show Toast, nó sẽ cho kết quả bánh mì nướng tùy chỉnh như hình dưới đây

làm cách nào để thiết kế tin nhắn bánh mì nướng tùy chỉnh trong Android?