যখন কোনো Activity ফোকাস পায়, তখন অ্যান্ড্রয়েড ফ্রেমওয়ার্ক Activity তার লেআউট আঁকতে বলে। আঁকার প্রক্রিয়াটি অ্যান্ড্রয়েড ফ্রেমওয়ার্কই পরিচালনা করে, কিন্তু Activity অবশ্যই তার লেআউট হায়ারার্কির রুট নোডটি সরবরাহ করতে হয়।
অ্যান্ড্রয়েড ফ্রেমওয়ার্ক লেআউটের রুট নোড আঁকে এবং লেআউট ট্রি পরিমাপ ও অঙ্কন করে। এটি ট্রি বরাবর হেঁটে এবং অবৈধ অঞ্চলের সাথে ছেদকারী প্রতিটি View রেন্ডার করার মাধ্যমে অঙ্কন করে। প্রতিটি ViewGroup draw() মেথড ব্যবহার করে তার প্রতিটি চাইল্ডকে আঁকার জন্য অনুরোধ করার দায়িত্বে থাকে এবং প্রতিটি View নিজেকে আঁকার দায়িত্বে থাকে। যেহেতু ট্রি-টি প্রি-অর্ডার পদ্ধতিতে ট্র্যাভার্স করা হয়, তাই ফ্রেমওয়ার্ক প্যারেন্টদেরকে তাদের চাইল্ডদের আগে—অন্য কথায়, পেছনে —আঁকে এবং সিবলিংদেরকে ট্রি-তে তাদের উপস্থিতির ক্রমানুসারে আঁকে।
অ্যান্ড্রয়েড ফ্রেমওয়ার্ক দুটি ধাপে লেআউট অঙ্কন করে: একটি মেজার পাস এবং একটি লেআউট পাস। ফ্রেমওয়ার্ক ` measure(int, int) মেজার পাসটি সম্পন্ন করে এবং View ট্রি-এর উপর থেকে নিচের দিকে ট্রাভার্সাল করে। এই রিকারশনের সময় প্রতিটি View ট্রি-এর নিচের দিকে ডাইমেনশন স্পেসিফিকেশনগুলো পাঠিয়ে দেয়। মেজার পাসের শেষে, প্রতিটি View তার পরিমাপগুলো সংরক্ষণ করে। ফ্রেমওয়ার্ক layout(int, int, int, int) ফাংশনে দ্বিতীয় পাসটি সম্পন্ন করে এবং এটিও উপর থেকে নিচের দিকে হয়। এই পাসের সময়, প্রতিটি প্যারেন্ট মেজার পাসে গণনা করা আকারগুলো ব্যবহার করে তার সমস্ত চাইল্ডকে পজিশন করার জন্য দায়ী থাকে।
লেআউট প্রক্রিয়ার দুটি ধাপ পরবর্তী বিভাগগুলিতে আরও বিস্তারিতভাবে বর্ণনা করা হয়েছে।
একটি পদক্ষেপ পাস শুরু করুন
যখন একটি View অবজেক্টের measure() মেথড রিটার্ন করে, তখন এর getMeasuredWidth() এবং getMeasuredHeight() ভ্যালুগুলো সেট করুন, সাথে View অবজেক্টটির সমস্ত ডিসেন্ডেন্টদের ভ্যালুগুলোও। একটি View অবজেক্টের পরিমাপ করা প্রস্থ এবং পরিমাপ করা উচ্চতার ভ্যালু অবশ্যই View প্যারেন্টদের দ্বারা আরোপিত সীমাবদ্ধতা মেনে চলতে হবে। এটি নিশ্চিত করতে সাহায্য করে যে, measure পাসের শেষে সমস্ত প্যারেন্ট তাদের চাইল্ডদের সমস্ত পরিমাপ গ্রহণ করে।
একটি প্যারেন্ট View তার চাইল্ড ভিউগুলোর উপর একাধিকবার measure() কল করতে পারে। উদাহরণস্বরূপ, প্যারেন্ট ভিউটি চাইল্ড ভিউগুলোর পছন্দের আকার নির্ধারণ করার জন্য অনির্দিষ্ট ডাইমেনশন দিয়ে একবার তাদের মাপ নিতে পারে। যদি চাইল্ড ভিউগুলোর অনিয়ন্ত্রিত আকারের যোগফল খুব বেশি বা খুব কম হয়, তবে প্যারেন্ট ভিউটি এমন মান দিয়ে আবার measure() ফাংশন কল করতে পারে যা চাইল্ড ভিউগুলোর আকারকে সীমাবদ্ধ করে।
মেজার পাস ডাইমেনশন বা মাত্রা জানানোর জন্য দুটি ক্লাস ব্যবহার করে। ViewGroup.LayoutParams ক্লাসের মাধ্যমেই View অবজেক্টগুলো তাদের পছন্দের আকার ও অবস্থান জানায়। মূল ViewGroup.LayoutParams ক্লাসটি View এর পছন্দের প্রস্থ এবং উচ্চতা বর্ণনা করে। প্রতিটি ডাইমেনশনের জন্য, এটি নিম্নলিখিতগুলির মধ্যে একটি নির্দিষ্ট করতে পারে:
- একটি সঠিক মাত্রা।
-
MATCH_PARENT, যার অর্থ হলোViewজন্য পছন্দের আকার হবে এর প্যারেন্টের আকারের সমান, প্যাডিং বাদ দিয়ে। -
WRAP_CONTENT, যার অর্থ হলোViewএর জন্য পছন্দের আকারটি তার বিষয়বস্তুকে আবদ্ধ করার জন্য যথেষ্ট বড়, সাথে প্যাডিংও থাকে।
ViewGroup এর বিভিন্ন সাবক্লাসের জন্য ViewGroup.LayoutParams এর সাবক্লাস রয়েছে। উদাহরণস্বরূপ, RelativeLayout এর নিজস্ব ViewGroup.LayoutParams সাবক্লাস আছে, যার মধ্যে চাইল্ড View অবজেক্টগুলোকে আনুভূমিকভাবে এবং উল্লম্বভাবে কেন্দ্র করার ক্ষমতা অন্তর্ভুক্ত।
MeasureSpec অবজেক্টগুলো প্যারেন্ট থেকে চাইল্ড ট্রি-তে রিকোয়ারমেন্ট পুশ করতে ব্যবহৃত হয়। একটি MeasureSpec তিনটি মোডের যেকোনো একটিতে থাকতে পারে:
-
UNSPECIFIED): প্যারেন্ট একটি চাইল্ডViewএর টার্গেট ডাইমেনশন নির্ধারণ করতে এটি ব্যবহার করে। উদাহরণস্বরূপ, একটিLinearLayoutতার চাইল্ড ভিউ-এর উচ্চতাUNSPECIFIEDএবং প্রস্থEXACTLY২৪০ পিক্সেল সেট করেmeasure()ফাংশন কল করতে পারে, এটি জানার জন্য যে ২৪০ পিক্সেল প্রস্থের সাপেক্ষে চাইল্ডViewকতটা লম্বা হতে চায়। -
EXACTLY: প্যারেন্ট এটি ব্যবহার করে চাইল্ডের উপর একটি সুনির্দিষ্ট আকার আরোপ করে। চাইল্ডকে অবশ্যই এই আকারটি ব্যবহার করতে হবে এবং নিশ্চিত করতে হবে যে তার সমস্ত ডিসেন্ডেন্ট এই আকারের মধ্যেই থাকে। -
AT MOST: প্যারেন্ট এটি ব্যবহার করে চাইল্ডের উপর একটি সর্বোচ্চ আকার আরোপ করে। চাইল্ডকে অবশ্যই নিশ্চিত করতে হবে যে সে নিজে এবং তার সমস্ত ডিসেন্ডেন্ট এই আকারের মধ্যে থাকে।
একটি লেআউট পাস শুরু করুন
একটি লেআউট শুরু করতে, requestLayout() কল করুন। এই মেথডটি সাধারণত একটি View নিজের উপরই কল করে, যখন এটি মনে করে যে এটি আর তার সীমার মধ্যে আঁটতে পারবে না।
একটি কাস্টম পরিমাপ এবং লেআউট লজিক প্রয়োগ করুন
আপনি যদি কোনো কাস্টম পরিমাপ বা লেআউট লজিক প্রয়োগ করতে চান, তাহলে যে মেথডগুলোতে লজিকটি প্রয়োগ করা হয়েছে, সেই মেথডগুলো ওভাররাইড করুন: onMeasure(int, int) এবং onLayout(boolean, int, int, int, int) । এই মেথডগুলো যথাক্রমে measure(int, int) এবং layout(int, int, int, int) দ্বারা কল করা হয়। measure(int, int) বা layout(int, int) মেথডগুলো ওভাররাইড করার চেষ্টা করবেন না—এই দুটি মেথডই final , তাই এগুলো ওভাররাইড করা যায় না।
নিম্নলিখিত উদাহরণটি দেখায় যে কীভাবে WindowManager স্যাম্পল অ্যাপের `SplitLayout` ক্লাসে এটি করতে হয়। যদি SplitLayout দুই বা ততোধিক চাইল্ড ভিউ থাকে এবং ডিসপ্লেতে একটি ফোল্ড থাকে, তাহলে এটি দুটি চাইল্ড ভিউকে ফোল্ডের উভয় পাশে স্থাপন করে। নিম্নলিখিত উদাহরণটি মেজারমেন্ট এবং লেআউট ওভাররাইড করার একটি ব্যবহার দেখায়, কিন্তু প্রোডাকশনের জন্য, আপনি যদি এই আচরণটি চান তবে SlidingPaneLayout ব্যবহার করুন।
কোটলিন
/** * An example of split-layout for two views, separated by a display * feature that goes across the window. When both start and end views are * added, it checks whether there are display features that separate the area * in two—such as a fold or hinge—and places them side-by-side or * top-bottom. */ class SplitLayout : FrameLayout { private var windowLayoutInfo: WindowLayoutInfo? = null private var startViewId = 0 private var endViewId = 0 private var lastWidthMeasureSpec: Int = 0 private var lastHeightMeasureSpec: Int = 0 ... fun updateWindowLayout(windowLayoutInfo: WindowLayoutInfo) { this.windowLayoutInfo = windowLayoutInfo requestLayout() } override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { val startView = findStartView() val endView = findEndView() val splitPositions = splitViewPositions(startView, endView) if (startView != null && endView != null && splitPositions != null) { val startPosition = splitPositions[0] val startWidthSpec = MeasureSpec.makeMeasureSpec(startPosition.width(), EXACTLY) val startHeightSpec = MeasureSpec.makeMeasureSpec(startPosition.height(), EXACTLY) startView.measure(startWidthSpec, startHeightSpec) startView.layout( startPosition.left, startPosition.top, startPosition.right, startPosition.bottom ) val endPosition = splitPositions[1] val endWidthSpec = MeasureSpec.makeMeasureSpec(endPosition.width(), EXACTLY) val endHeightSpec = MeasureSpec.makeMeasureSpec(endPosition.height(), EXACTLY) endView.measure(endWidthSpec, endHeightSpec) endView.layout( endPosition.left, endPosition.top, endPosition.right, endPosition.bottom ) } else { super.onLayout(changed, left, top, right, bottom) } } /** * Gets the position of the split for this view. * @return A rect that defines of split, or {@code null} if there is no split. */ private fun splitViewPositions(startView: View?, endView: View?): Array? { if (windowLayoutInfo == null || startView == null || endView == null) { return null } // Calculate the area for view's content with padding. val paddedWidth = width - paddingLeft - paddingRight val paddedHeight = height - paddingTop - paddingBottom windowLayoutInfo?.displayFeatures ?.firstOrNull { feature -> isValidFoldFeature(feature) } ?.let { feature -> getFeaturePositionInViewRect(feature, this)?.let { if (feature.bounds.left == 0) { // Horizontal layout. val topRect = Rect( paddingLeft, paddingTop, paddingLeft + paddedWidth, it.top ) val bottomRect = Rect( paddingLeft, it.bottom, paddingLeft + paddedWidth, paddingTop + paddedHeight ) if (measureAndCheckMinSize(topRect, startView) && measureAndCheckMinSize(bottomRect, endView) ) { return arrayOf(topRect, bottomRect) } } else if (feature.bounds.top == 0) { // Vertical layout. val leftRect = Rect( paddingLeft, paddingTop, it.left, paddingTop + paddedHeight ) val rightRect = Rect( it.right, paddingTop, paddingLeft + paddedWidth, paddingTop + paddedHeight ) if (measureAndCheckMinSize(leftRect, startView) && measureAndCheckMinSize(rightRect, endView) ) { return arrayOf(leftRect, rightRect) } } } } // You previously tried to fit the children and measure them. Since they // don't fit, measure again to update the stored values. measure(lastWidthMeasureSpec, lastHeightMeasureSpec) return null } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) lastWidthMeasureSpec = widthMeasureSpec lastHeightMeasureSpec = heightMeasureSpec } /** * Measures a child view and sees if it fits in the provided rect. * This method calls [View.measure] on the child view, which updates its * stored values for measured width and height. If the view ends up with * different values, measure again. */ private fun measureAndCheckMinSize(rect: Rect, childView: View): Boolean { val widthSpec = MeasureSpec.makeMeasureSpec(rect.width(), AT_MOST) val heightSpec = MeasureSpec.makeMeasureSpec(rect.height(), AT_MOST) childView.measure(widthSpec, heightSpec) return childView.measuredWidthAndState and MEASURED_STATE_TOO_SMALL == 0 && childView.measuredHeightAndState and MEASURED_STATE_TOO_SMALL == 0 } private fun isValidFoldFeature(displayFeature: DisplayFeature) = (displayFeature as? FoldingFeature)?.let { feature -> getFeaturePositionInViewRect(feature, this) != null } ?: false }
জাভা
/** * An example of split-layout for two views, separated by a display feature * that goes across the window. When both start and end views are added, it checks * whether there are display features that separate the area in two—such as * fold or hinge—and places them side-by-side or top-bottom. */ public class SplitLayout extends FrameLayout { @Nullable private WindowLayoutInfo windowLayoutInfo = null; private int startViewId = 0; private int endViewId = 0; private int lastWidthMeasureSpec = 0; private int lastHeightMeasureSpec = 0; ... void updateWindowLayout(WindowLayoutInfo windowLayoutInfo) { this.windowLayoutInfo = windowLayoutInfo; requestLayout(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { @Nullable View startView = findStartView(); @Nullable View endView = findEndView(); @Nullable ListsplitPositions = splitViewPositions(startView, endView); if (startView != null && endView != null && splitPositions != null) { Rect startPosition = splitPositions.get(0); int startWidthSpec = MeasureSpec.makeMeasureSpec(startPosition.width(), EXACTLY); int startHeightSpec = MeasureSpec.makeMeasureSpec(startPosition.height(), EXACTLY); startView.measure(startWidthSpec, startHeightSpec); startView.layout( startPosition.left, startPosition.top, startPosition.right, startPosition.bottom ); Rect endPosition = splitPositions.get(1); int endWidthSpec = MeasureSpec.makeMeasureSpec(endPosition.width(), EXACTLY); int endHeightSpec = MeasureSpec.makeMeasureSpec(endPosition.height(), EXACTLY); startView.measure(endWidthSpec, endHeightSpec); startView.layout( endPosition.left, endPosition.top, endPosition.right, endPosition.bottom ); } else { super.onLayout(changed, left, top, right, bottom); } } /** * Gets the position of the split for this view. * @return A rect that defines of split, or {@code null} if there is no split. */ @Nullable private List splitViewPositions(@Nullable View startView, @Nullable View endView) { if (windowLayoutInfo == null || startView == null || endView == null) { return null; } int paddedWidth = getWidth() - getPaddingLeft() - getPaddingRight(); int paddedHeight = getHeight() - getPaddingTop() - getPaddingBottom(); List displayFeatures = windowLayoutInfo.getDisplayFeatures(); @Nullable DisplayFeature feature = displayFeatures .stream() .filter(item -> isValidFoldFeature(item) ) .findFirst() .orElse(null); if (feature != null) { Rect position = SampleToolsKt.getFeaturePositionInViewRect(feature, this, true); Rect featureBounds = feature.getBounds(); if (featureBounds.left == 0) { // Horizontal layout. Rect topRect = new Rect( getPaddingLeft(), getPaddingTop(), getPaddingLeft() + paddedWidth, position.top ); Rect bottomRect = new Rect( getPaddingLeft(), position.bottom, getPaddingLeft() + paddedWidth, getPaddingTop() + paddedHeight ); if (measureAndCheckMinSize(topRect, startView) && measureAndCheckMinSize(bottomRect, endView)) { ArrayList rects = new ArrayList (); rects.add(topRect); rects.add(bottomRect); return rects; } } else if (featureBounds.top == 0) { // Vertical layout. Rect leftRect = new Rect( getPaddingLeft(), getPaddingTop(), position.left, getPaddingTop() + paddedHeight ); Rect rightRect = new Rect( position.right, getPaddingTop(), getPaddingLeft() + paddedWidth, getPaddingTop() + paddedHeight ); if (measureAndCheckMinSize(leftRect, startView) && measureAndCheckMinSize(rightRect, endView)) { ArrayList rects = new ArrayList (); rects.add(leftRect); rects.add(rightRect); return rects; } } } // You previously tried to fit the children and measure them. Since // they don't fit, measure again to update the stored values. measure(lastWidthMeasureSpec, lastHeightMeasureSpec); return null; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); lastWidthMeasureSpec = widthMeasureSpec; lastHeightMeasureSpec = heightMeasureSpec; } /** * Measures a child view and sees if it fits in the provided rect. * This method calls [View.measure] on the child view, which updates * its stored values for measured width and height. If the view ends up with * different values, measure again. */ private boolean measureAndCheckMinSize(Rect rect, View childView) { int widthSpec = MeasureSpec.makeMeasureSpec(rect.width(), AT_MOST); int heightSpec = MeasureSpec.makeMeasureSpec(rect.height(), AT_MOST); childView.measure(widthSpec, heightSpec); return (childView.getMeasuredWidthAndState() & MEASURED_STATE_TOO_SMALL) == 0 && (childView.getMeasuredHeightAndState() & MEASURED_STATE_TOO_SMALL) == 0; } private boolean isValidFoldFeature(DisplayFeature displayFeature) { if (displayFeature instanceof FoldingFeature) { return SampleToolsKt.getFeaturePositionInViewRect(displayFeature, this, true) != null; } else { return false; } } }
