ตัวอย่างนี้สาธิตวิธีสร้างขอบ (Border) เฉพาะด้านบนและด้านล่างของ View โดยใช้ Layer List Drawable XML ไม่ต้องเขียนโค้ด Java
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project
ขั้นตอนที่ 2: สร้าง Drawable Border (res/drawable/border_top_bottom.xml)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<!-- Layer 1: พื้นหลังเต็ม + ขอบทั้ง 4 ด้าน -->
<item>
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#e10606" />
<solid android:color="#9bce64" />
</shape>
</item>
<!-- Layer 2: ทับซ้อนกลาง เพื่อซ่อนขอบซ้าย-ขวา (เหลือแค่บน-ล่าง) -->
<item android:bottom="2dp" android:top="2dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#9bce64" />
<!-- สี stroke ตรงกับพื้นหลัง Layer 1 จะทำให้ขอบซ้าย-ขวาหายไป -->
</shape>
</item>
</layer-list>
คำอธิบายเทคนิค Layer List
- Item แรก: วาดสี่เหลี่ยมเต็มพร้อมขอบสีแดง (#e10606) และพื้นหลังสีเขียว (#9bce64)
- Item ที่สอง: ใช้
android:top="2dp"และandroid:bottom="2dp"เพื่อย่อขนาดลง 2dp ด้านบน-ล่าง → ทับซ้อนตรงกลาง - Stroke ของ Item ที่สองใช้สีเดียวกับพื้นหลัง (#9bce64) → ทำให้ขอบซ้าย-ขวาของ Item แรกถูกบัง
- ผลลัพธ์: เหลือขอบด้านบนและด้านล่างสีแดง ส่วนซ้าย-ขวาไม่มีขอบ
ขั้นตอนที่ 3: Layout ใช้งาน (res/layout/top_bottom_border_in_android_view.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="Adding Border in Top and Bottom of an Android View" />
<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="Top Bottom Border in TextView"
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="Top Bottom Border in Button" />
</LinearLayout>
ขั้นตอนที่ 4: Strings (res/values/strings.xml)
<resources>
<string name="app_name">Adding Border to an Android View</string>
</resources>
ขั้นตอนที่ 5: 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);
// ใช้ Layout ที่สร้างไว้ข้างต้น
setContentView(R.layout.top_bottom_border_in_android_view);
}
}
ขั้นตอนที่ 6: 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>
<dist:module dist:instant="true" />
</manifest>
การปรับแต่งเพิ่มเติม
ขอบด้านซ้าย-ขวา แทน บน-ล่าง
<item android:left="2dp" android:right="2dp">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#9bce64" />
</shape>
</item>
ขอบแบบจุด (Dashed) - ต้องใช้ Code
XML ไม่รองรับ Dashed Stroke ต้องใช้ PathEffect ใน Java/Kotlin:
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4);
paint.setPathEffect(new DashPathEffect(new float[]{10, 5}, 0));
การทดสอบ
รันแอป → จะเห็น TextView และ Button มีขอบสีแดงด้านบนและด้านล่างเท่านั้น