Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีจัดวาง Views ใน RelativeLayout ด้วยโค้ด (Programmatically) ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการสร้างและจัดวาง View (ปุ่ม) ใน RelativeLayout โดยเขียนโค้ด Java โดยตรง แทนที่จะใช้ XML

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relativelayoutID"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (MainActivity.java)

package com.app.sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
    RelativeLayout relativelayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // สร้างปุ่มแบบไดนามิก
        Button dynamicButton = new Button(MainActivity.this);
        relativelayout = findViewById(R.id.relativelayoutID);
        
        // กำหนด LayoutParams และกฎการจัดวาง
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        );
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        
        dynamicButton.setLayoutParams(layoutParams);
        dynamicButton.setText("Dynamic Button");
        relativelayout.addView(dynamicButton);
        
        // เพิ่ม Event Listener
        dynamicButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Clicked on Dynamic button", Toast.LENGTH_LONG).show();
            }
        });
    }
}

ขั้นตอนที่ 4: ตั้งค่า Manifest

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

การทดสอบ

รันแอปพลิเคชันบนอุปกรณ์จริง จะเห็นปุ่ม "Dynamic Button" อยู่ตรงกลางหน้าจอ กดปุ่มจะแสดง Toast

วิธีจัดวาง Views ใน RelativeLayout ด้วยโค้ด (Programmatically) ใน Android

คำอธิบายเพิ่มเติม

  • LayoutParams: กำหนดขนาดและกฎการจัดวาง (Rules)
  • addRule(): ใช้สำหรับ RelativeLayout เช่น CENTER_IN_PARENT, BELOW, ALIGN_PARENT_RIGHT
  • addView(): เพิ่ม View ลงใน ViewGroup