ภาพรวม
ตัวอย่างนี้สาธิตการเปลี่ยนขนาด View (Button) อย่างไดนามิกผ่านโค้ด Java โดยใช้ LayoutParams แทน layout_weight โดยตรง
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio → File → New Project และกรอกข้อมูลที่จำเป็น
ขั้นตอนที่ 2: เลย์เอาต์ (res/layout/activity_main.xml)
<?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"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:onClick="expand"
android:textSize="24sp"
android:text="Hello World"
android:layout_centerInParent="true" />
</RelativeLayout>
ขั้นตอนที่ 3: โค้ดหลัก (src/MainActivity.java)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void expand(View view) {
int height = RelativeLayout.LayoutParams.MATCH_PARENT;
int width = 500; // px - ควรใช้ dp แปลงเป็น px
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(width, height);
button = findViewById(R.id.button);
button.setLayoutParams(layoutParams);
}
}
ขั้นตอนที่ 4: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.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>
การทดสอบ
รันแอป กดปุ่ม "Hello World" ปุ่มจะขยายขนาดตามที่ตั้งค่าใน expand()

การใช้ LayoutParams ให้ถูกต้อง
1. ต้องใช้ LayoutParams ที่ตรงกับ Parent ViewGroup
| Parent | LayoutParams Class |
|---|---|
| LinearLayout | LinearLayout.LayoutParams |
| RelativeLayout | RelativeLayout.LayoutParams |
| ConstraintLayout | ConstraintLayout.LayoutParams |
| FrameLayout | FrameLayout.LayoutParams |
| GridLayout | GridLayout.LayoutParams |
2. ตั้งค่า weight ใน LinearLayout
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
0, // width = 0 เมื่อใช้ weight
LinearLayout.LayoutParams.WRAP_CONTENT,
1.0f // weight = 1
);
view.setLayoutParams(params);
3. แปลง dp เป็น px (แนะนำ)
public int dpToPx(int dp) {
float density = getResources().getDisplayMetrics().density;
return Math.round(dp * density);
}
// ใช้งาน
int width = dpToPx(200); // 200 dp
4. กรณี ConstraintLayout (ซับซ้อนกว่า)
ConstraintLayout.LayoutParams params =
(ConstraintLayout.LayoutParams) view.getLayoutParams();
params.width = dpToPx(200);
params.height = ConstraintLayout.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(params);
// หรือใช้ ConstraintSet สำหรับ constraint ซับซ้อน
ConstraintSet set = new ConstraintSet();
set.clone(constraintLayout);
set.constrainWidth(view.getId(), dpToPx(200));
set.applyTo(constraintLayout);
💡 หมายเหตุ: ตัวอย่างเดิมใช้ width = 500 px โดยตรง ซึ่งจะแสดงผลต่างกันบนหน้าจอความหนาแน่นต่างกัน ควรใช้ dpToPx() เสมอ