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

วิธีหาขนาด View ในขณะรันโปรแกรม (Runtime) ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีได้รับขนาด (Width/Height) ของ View จริงหลังจาก Layout เสร็จสิ้น โดยใช้ ViewTreeObserver.OnGlobalLayoutListener

ขั้นตอนที่ 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"
    android:padding="8dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_gravity="center"
        android:layout_margin="20sp"
        android:src="@drawable/image"/>

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

</RelativeLayout>

ขั้นตอนที่ 3: เพิ่มรูปภาพ

คัดลอกไฟล์รูปภาพ (.png, .jpg, .jpeg) ไปวางใน res/drawable/ ชื่อ image

ขั้นตอนที่ 4: โค้ดหลัก (src/MainActivity.java)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

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);
        imageView.getViewTreeObserver().addOnGlobalLayoutListener(new GetViewSize());
    }

    class GetViewSize implements ViewTreeObserver.OnGlobalLayoutListener {
        @Override
        public void onGlobalLayout() {
            View v = findViewById(R.id.imageView);
            String width = Integer.toString(v.getWidth());
            String height = Integer.toString(v.getHeight());
            ((TextView) findViewById(R.id.textView)).setText(String.format("Width %s, Height: %s", width, height));

            // สำคัญ: ลบ Listener หลังใช้งานครั้งแรกเพื่อไม่ให้ทำงานซ้ำ
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    }
}

ขั้นตอนที่ 5: Manifest

<?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>

การทดสอบ

รันแอปบนอุปกรณ์จริง - จะแสดงขนาด Width และ Height จริงของ ImageView ใน TextView

จุดสำคัญ

  • onGlobalLayout() เรียกหลัง Layout เสร็จสิ้น - ขนาด View จึงพร้อมใช้งาน
  • ควรลบ Listener หลังใช้งานเพื่อป้องกันการทำงานซ้ำและ Memory Leak
  • วิธีนี้ใช้ได้กับ View ทุกชนิด ไม่ใช่แค่ ImageView