ภาพรวม
บทความนี้สาธิตวิธีดึงค่าความสูงและความกว้างของ Navigation Bar (แถบนำทางระบบ) ใน Android ด้วยโค้ดโปรแกรม ซึ่งจำเป็นสำหรับการจัด Layout ให้พอดีกับหน้าจอ โดยเฉพาะอุปกรณ์ที่มี Gesture Navigation หรือ Notch
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
สร้างโปรเจกต์ Android ใหม่ใน Android Studio
ขั้นตอนที่ 2: ออกแบบ Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ขนาด Navigation Bar ของ Android"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
ขั้นตอนที่ 3: เขียนโค้ด MainActivity (src/MainActivity.java) - เวอร์ชันแก้ไข
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView);
// วิธีที่ 1: ใช้ Resource ID (สำหรับ Navigation Bar แบบดั้งเดิม 3 ปุ่ม)
Resources resources = getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
int navBarHeight = 0;
if (resourceId > 0) {
navBarHeight = resources.getDimensionPixelSize(resourceId);
}
// วิธีที่ 2: ใช้ WindowInsets (API 20+ / Android 5.0+) - แนะนำ
View decorView = getWindow().getDecorView();
WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
int navBarHeightModern = 0;
if (rootWindowInsets != null) {
navBarHeightModern = rootWindowInsets.getSystemWindowInsetBottom();
}
// วิธีที่ 3: ใช้ WindowInsetsController (API 30+ / Android 11+)
int navBarHeightApi30 = 0;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
WindowInsetsController controller = getWindow().getInsetsController();
if (controller != null) {
WindowInsets insets = controller.getSystemWindowInsets();
if (insets != null) {
navBarHeightApi30 = insets.getSystemWindowInsetBottom();
}
}
}
// แสดงผล
String result = String.format(
"Resource ID Method: %d px\nWindowInsets Method: %d px\nAPI 30+ Method: %d px",
navBarHeight, navBarHeightModern, navBarHeightApi30
);
textView.setText(result);
}
}
ขั้นตอนที่ 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>
คำอธิบายเพิ่มเติม
ความแตกต่างระหว่างวิธีต่างๆ
- Resource ID (navigation_bar_height): ใช้ได้กับ Navigation Bar แบบ 3 ปุ่มดั้งเดิม ไม่รองรับ Gesture Navigation
- WindowInsets (ViewCompat.getRootWindowInsets): รองรับทุกรูปแบบ Navigation (3 ปุ่ม, Gesture, Notch) - แนะนำให้ใช้วิธีนี้
- WindowInsetsController (API 30+): API ใหม่สุด สำหรับควบคุม System Bars ได้ละเอียดกว่า
การใช้งานจริงกับ Layout
// ปรับ padding ให้ View ไม่ถูก Navigation Bar บัง
view.setOnApplyWindowInsetsListener((v, insets) -> {
int bottomInset = insets.getSystemWindowInsetBottom();
v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), bottomInset);
return insets.consumeSystemWindowInsets();
});
ค่าทั่วไป (Reference)
- Navigation Bar 3 ปุ่ม: ~48-56 dp (72-84 px บน xxhdpi)
- Gesture Navigation Handle: ~24-32 dp
- ค่าอาจแตกต่างตาม OEM (Samsung, Xiaomi, Pixel, etc.)