如何在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" android:id="@+id/parent" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:gravity="center" android:background="#33FFFF00" android:orientation="vertical"> <TextView android:id="@+id/text" android:textSize="18sp" android:textAlignment="center" android:text="click to show toast at top" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:layout_width="match_parent" android:layout_marginTop="10dp" android:layout_height="5dp" android:src="@drawable/dotted" android:layerType="software" /> </LinearLayout>
在上面的程式碼中,我們有一個帶有影像檢視的文字檢視。影像檢視包含一個點狀背景。因此,如下所示在drawable中建立dotted.xml -
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <solid android:color="@android:color/white" /> <stroke android:width="1dip" android:color="#4fa5d5"/> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp"/> </shape>
步驟3 - 將以下程式碼新增到src/MainActivity.java
package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int view = R.layout.activity_main;
TextView text;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
text = findViewById(R.id.text);
text.setText("Dotted line for text view ");
}
}讓我們嘗試執行您的應用程式。我假設您已將實際的Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟專案中的一個活動檔案,然後單擊執行
工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

以上結果包含一條點線。
點選 這裡 下載專案程式碼
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP