如何在 Android TextView 周圍新增邊框?
如果您想將文字檢視顯示為 3D 檢視,就像我們在 Microsoft PowerPoint 中看到的 3D 文字一樣。此示例演示瞭如何在 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" tools:context=".MainActivity" android:background="#dde4dd" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/text" android:textSize="18sp" android:layout_width="wrap_content" android:background="@drawable/ border " android:layout_height="wrap_content" /> </LinearLayout>
在上面的程式碼中,我們使用邊框作為背景獲取了一個文字檢視,因此我們需要在 drawable 中建立一個名為 boarder.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>
在上面的程式碼中,我們使用 shape 作為根標籤,並指定形狀為矩形,併為形狀添加了寬度和填充。
步驟 3 - 將以下程式碼新增到 src/MainActivity.java 中
package com.example.andy.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.w3c.dom.Text; import java.util.Locale; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView textView=findViewById(R.id.text); textView.setText("Tutorialspoint india pvt ltd"); } }
讓我們嘗試執行您的應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要從 Android Studio 執行應用程式,請開啟您專案中的一個活動檔案,然後單擊工具欄中的執行圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -
在以上結果中,我們在文字檢視中添加了邊框。
點選 這裡 下載專案程式碼
廣告