如何在 Android View 的頂部和底部新增邊框?
此示例演示如何在 Android View 的頂部和底部新增邊框。
步驟 1 − 在 Android Studio 中建立一個新專案,轉到 檔案 ⇒ 新建專案,並填寫所有必需的詳細資訊以建立新專案。
步驟 2 − 將以下程式碼新增到 res/drawable/border_top_bottom.xml
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <stroke android:width="2dp" android:color="#e10606" /> <solid android:color="#9bce64" /> </shape> </item> <item android:bottom="2dp" android:top="2dp"> <shape android:shape="rectangle"> <stroke android:width="2dp" android:color="#9bce64" /> </shape> </item> </layer-list>
步驟 3 − 將以下程式碼新增到 res/layout/top_bottom_border_in_android_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="16dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Adding Border in Top and Bottom of an Android View" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:background="@drawable/border_top_bottom" android:padding="30dp" android:text="Top Bottom Border in TextView" android:textColor="#000000" android:textSize="18sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:background="@drawable/border_top_bottom" android:text="Top Bottom Border in Button" /> </LinearLayout>
步驟 4 − 將以下程式碼新增到 res/layout/strings.xml
<resources> <string name="app_name">Adding Border to an Android View</string> </resources>
步驟 5 − 將以下程式碼新增到 res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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=".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" /> </androidx.constraintlayout.widget.ConstraintLayout>
步驟 6 − 將以下程式碼新增到 src/MainActivity.java
package com.example.sample1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.top_bottom_border_in_android_view);
}
}步驟 7 − 將以下程式碼新增到 Manifests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:dist="http://schemas.android.com/apk/distribution" package="com.example.sample1"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <dist:module dist:instant="true" /> </manifest>
讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要從 Android Studio 執行應用程式,請開啟專案中的一個活動檔案,然後單擊執行
工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 –

點選 此處 下載專案程式碼。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP