Tạo border cho TextView bằng Drawable XML
Android không có thuộc tính border trực tiếp cho View. Cách phổ biến là tạo file drawable shape dạng rectangle với stroke (viền) và set làm background cho TextView.
Bước 1: Tạo file drawable/border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- Màu nền bên trong -->
<solid android:color="@android:color/white" />
<!-- Viền: độ dày 1dp, màu xanh -->
<stroke
android:width="1dp"
android:color="#4FA5D5" />
<!-- Padding bên trong viền -->
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
Bước 2: Layout activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#DDE4DD">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:background="@drawable/border" />
</LinearLayout>
Bước 3: MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
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");
}
}
Mở rộng: Viền bo góc (Rounded Corners)
Thêm tag <corners android:radius="8dp" /> vào file shape để bo tròn góc.