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

วิธีดึงความสูงของ Action Bar ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการดึงความสูงของ Action Bar (ToolBar) ใน Android โดยใช้ Toolbar widget และเมธอด getHeight()

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

เปิด Android Studio → File → New Project → กรอกรายละเอียดที่จำเป็น

ขั้นตอนที่ 2: จัดทำ Layout (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"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        android:background="@color/colorAccent">
    </android.support.v7.widget.Toolbar>

    <Button
        android:id="@+id/btnGetHeight"
        android:text="ดูความสูง Action Bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <TextView
        android:id="@+id/textView"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_marginTop="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/btnGetHeight" />

</RelativeLayout>

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

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;
    Button button;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = findViewById(R.id.actionBarSize);
        toolbar.setTitle("MyActionBar");
        setSupportActionBar(toolbar);

        textView = findViewById(R.id.textView);
        button = findViewById(R.id.btnGetHeight);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // getHeight() จะคืนค่า 0 หากเรียกใน onCreate (Layout ยังไม่เสร็จ)
                // ต้องเรียกหลังจาก Layout เสร็จ (เช่นใน OnClickListener)
                int height = toolbar.getHeight();
                textView.setText("ความสูง ActionBar: " + height + " pixels");
            }
        });
    }
}

ขั้นตอนที่ 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>

วิธีอื่นๆ ในการดึงความสูง Action Bar

// 1. จาก Theme Attribute (ทำงานได้ใน onCreate)
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
    actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
}

// 2. จาก Toolbar (หลัง Layout เสร็จ)
toolbar.post(new Runnable() {
    @Override
    public void run() {
        int height = toolbar.getHeight(); // ความสูงจริงหลัง render
    }
});

// 3. ใช้ ViewTreeObserver
toolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        toolbar.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        int height = toolbar.getHeight();
    }
});

การทดสอบ

รันแอปบนอุปกรณ์จริง กดปุ่ม "ดูความสูง Action Bar\" จะแสดงความสูงเป็น Pixel

วิธีดึงความสูงของ Action Bar ใน Android

หมายเหตุ: ความสูงมาตรฐานของ Action Bar คือ 56dp (Material Design) แต่อาจแตกต่างไปตาม Theme และ Device