如何在 Android 中繪製三角形形狀?


此示例演示如何在 Android 中繪製三角形形狀。

步驟 1 − 在 Android Studio 中建立一個新專案,轉到檔案 ⇒ 新建專案,然後填寫所有必需的詳細資訊來建立新專案。

步驟 2 − 將以下程式碼新增到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:gravity="center"
   android:layout_marginTop="30dp"
   tools:context=".MainActivity">
   <ImageView
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:background="@drawable/background"/>
</LinearLayout>

在上述程式碼中,我們已採用影像檢視,並將背景新增為 background.xml。

步驟 3 − 將以下程式碼新增到 drawable/background.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
   android:height="100dp"
   android:width="100dp"
   android:viewportWidth="24"
   android:viewportHeight="24">
   <path android:fillColor="#000" android:pathData="M1,21H23L12,2" />
</vector>

讓我們嘗試執行你的應用程式。我假設你已將實際的安卓移動裝置連線至你的電腦。要從 Android Studio 執行應用,請開啟你的一個專案活動檔案,然後單擊工具欄中的執行  圖示。選擇你的移動裝置作為選項,然後檢視將顯示預設螢幕的移動裝置 –

單擊 此處下載專案程式碼

更新日期: 2020 年 06 月 27 日

2K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始進行
廣告