Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách thêm viền trên và dưới cho View trong Android

Hướng dẫn này minh họa cách tạo viền chỉ ở trên và dưới (top/bottom border) cho bất kỳ View nào bằng layer-list drawable.

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project.

Bước 2: Tạo drawable viền (res/drawable/border_top_bottom.xml)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
    <!-- Lớp dưới: viền đầy đủ -->
    <item>
        <shape android:shape="rectangle">
            <stroke
                android:width="2dp"
                android:color="#e10606" />
            <solid android:color="#9bce64" />
        </shape>
    </item>

    <!-- Lớp trên: che viền trái/phải bằng màu nền, chỉ còn trên/dưới -->
    <item android:top="2dp" android:bottom="2dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="2dp"
                android:color="#9bce64" />
            <solid android:color="#9bce64" />
        </shape>
    </item>
</layer-list>

Bước 3: Layout demo (res/layout/top_bottom_border_demo.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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="Demo viền trên/dưới cho View"
        android:textSize="16sp"
        android:layout_marginBottom="16dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:background="@drawable/border_top_bottom"
        android:padding="30dp"
        android:text="TextView có viền trên/dưới"
        android:textColor="#000000"
        android:textSize="18sp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:background="@drawable/border_top_bottom"
        android:text="Button có viền trên/dưới" />

</LinearLayout>

Bước 4: Strings (res/values/strings.xml)

<resources>
    <string name="app_name">Viền trên/dưới Android View</string>
</resources>

Bước 5: Activity chính (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_demo);
    }
}

Bước 6: Manifest (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:dist="https://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>
</manifest>

💡 Mở rộng: Viền 4 cạnh, viền tròn, gradient

<!-- Viền 4 cạnh đơn giản -->
<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <stroke android:width="2dp" android:color="#FF0000" />
    <solid android:color="#FFFFFF" />
    <corners android:radius="8dp" />
</shape>

<!-- Viền gradient -->
<shape>
    <stroke android:width="2dp" />
    <gradient android:startColor="#FF0000" android:endColor="#0000FF" android:angle="45"/>
    <corners android:radius="12dp" />
</shape>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. TextView và Button sẽ hiển thị viền đỏ ở trên và dưới, nền xanh nhạt.