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

Nút Android tiêu chuẩn có màu khác?

Ví dụ này minh họa về Nút Android tiêu chuẩn có màu khác

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"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:gravity = "center"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:orientation = "vertical">
   <Button
      android:id = "@+id/parcleObject"
      android:layout_width = "wrap_content"
      android:layout_height = "wrap_content"
      android:layout_alignParentTop = "true"
      android:layout_centerHorizontal = "true"
      android:layout_marginTop = "27dp"
      android:background = "@drawable/background"
      android:text = "Click here"/>
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã sử dụng chế độ xem nút để hiển thị các màu khác nhau.

Bước 3 - Thêm mã sau vào drawable / background.xml

<?xml version = "1.0" encoding = "utf-8"?>
<selector
   xmlns:android = "https://schemas.android.com/apk/res/android">
   <item android:state_pressed = "true" >
      <shape>
         <gradient
            android:startColor = "#f0ff00"
            android:endColor = "#d9dd95"
            android:angle = "270" />
         <stroke
            android:width = "3dp"
            android:color = "#d1d3e3" />
         <corners
            android:radius = "3dp" />
         <padding
            android:left = "10dp"
            android:top = "10dp"
            android:right = "10dp"
            android:bottom = "10dp" />
      </shape>
   </item>
   <item android:state_focused = "true" >
      <shape>
         <gradient
            android:endColor = "#fd5900"
            android:startColor = "#eca680"
            android:angle = "270" />
         <stroke
            android:width = "3dp"
            android:color = "#d1d3e3" />
         <corners
            android:radius = "3dp" />
         <padding
            android:left = "10dp"
            android:top = "10dp"
            android:right = "10dp"
            android:bottom = "10dp" />
      </shape>
   </item>
   <item>
      <shape>
         <gradient
            android:endColor = "#4356e7"
            android:startColor = "#a1aaee"
            android:angle = "270" />
         <stroke
            android:width = "3dp"
            android:color = "#d1d3e3" />
         <corners
            android:radius = "3dp" />
         <padding
            android:left = "10dp"
            android:top = "10dp"
            android:right = "10dp"
            android:bottom = "10dp" />
      </shape>
   </item>
</selector>

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 -

Nút Android tiêu chuẩn có màu khác?

Bây giờ bấm vào nút, nó sẽ hiện ra kết quả như hình bên dưới -

Nút Android tiêu chuẩn có màu khác?