บทความนี้สาธิตวิธีตั้งค่า Margin ให้ ImageView โดยโปรแกรม (Programmatically) ผ่าน LayoutParams
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project
ขั้นตอนที่ 2: Layout (activity_main.xml)
ใช้ RelativeLayout เป็น Parent (เพราะ Margin ทำงานผ่าน Parent's LayoutParams):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/your_image" />
</RelativeLayout>
ขั้นตอนที่ 3: เตรียมรูปภาพ
คัดลอกไฟล์รูปภาพ (เช่น image.jpg) ไปวางใน res/drawable/
ขั้นตอนที่ 4: MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
// ต้อง Cast เป็น LayoutParams ของ Parent (RelativeLayout)
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
// ตั้งค่า Margin: left, top, right, bottom (หน่วยเป็น Pixel)
params.setMargins(100, 100, 100, 100);
// 適用การเปลี่ยนแปลง
imageView.setLayoutParams(params);
}
}
⚠️ จุดสำคัญ: LayoutParams ต้องตรงกับ Parent ViewGroup
| Parent ViewGroup | LayoutParams Class |
|---|---|
| LinearLayout | LinearLayout.LayoutParams |
| RelativeLayout | RelativeLayout.LayoutParams |
| FrameLayout | FrameLayout.LayoutParams |
| ConstraintLayout | ConstraintLayout.LayoutParams |
| CoordinatorLayout | CoordinatorLayout.LayoutParams |
ผิด: ใช้ ViewGroup.LayoutParams (Base class) - ไม่มีเมธอด setMargins
ถูก: Cast เป็น LayoutParams ของ Parent จริง
การแปลง dp เป็น Pixel
Margin ต้องเป็น Pixel ไม่ใช่ dp ต้องแปลง:
public int dpToPx(int dp) {
float density = getResources().getDisplayMetrics().density;
return Math.round(dp * density);
}
// ใช้งาน
params.setMargins(dpToPx(16), dpToPx(16), dpToPx(16), dpToPx(16));
Margin vs Padding
- Margin - ระยะห่าง นอก View (ระหว่าง View กับ Parent/อื่นๆ)
- Padding - ระยะห่าง ใน View (ระหว่างขอบ View กับ Content)
// Padding (ไม่ต้องใช้ LayoutParams) imageView.setPadding(dpToPx(16), dpToPx(16), dpToPx(16), dpToPx(16));
ConstraintLayout (ซับซ้อนกว่า)
ใช้ ConstraintSet แทน:
ConstraintLayout constraintLayout = findViewById(R.id.constraintLayout); ConstraintSet set = new ConstraintSet(); set.clone(constraintLayout); set.setMargin(R.id.imageView, ConstraintSet.START, dpToPx(16)); set.setMargin(R.id.imageView, ConstraintSet.TOP, dpToPx(16)); set.applyTo(constraintLayout);
Kotlin Extension (KTX) - สะดวกกว่า
// ต้อง import androidx.core.view.updateLayoutParams
imageView.updateLayoutParams<RelativeLayout.LayoutParams> {
setMargins(dpToPx(16), dpToPx(16), dpToPx(16), dpToPx(16))
}