Android çerçevesi, Activity odaklandığında düzenini çizmesi için Activity isteğinde bulunur. Çizim prosedürü Android çerçevesi tarafından işlenir ancak Activity, düzen hiyerarşisinin kök düğümünü sağlamalıdır.
Android çerçevesi, düzenin kök düğümünü çizer, düzen ağacını ölçer ve çizer. Ağaçta yürüyerek ve geçersiz bölgeyle kesişen her View öğesini oluşturarak çizer.
Her ViewGroup, draw() yöntemini kullanarak alt öğelerinin her birinin çizilmesini istemekten, her View ise kendisini çizmekten sorumludur. Ağaç, ön siparişle geçildiği için çerçeve, ebeveynleri çocuklarından önce, yani arkalarında çizer ve kardeşleri ağaçta göründükleri sırayla çizer.
Android çerçevesi, düzeni iki geçişli bir süreçte çizer: ölçü geçişi ve düzen geçişi. Çerçeve, ölçüm geçişini measure(int, int) içinde gerçekleştirir ve View ağacında yukarıdan aşağıya doğru geçiş yapar. Her View, özyineleme sırasında boyut özelliklerini ağaçta aşağı iter. Ölçüm geçişinin sonunda her View ölçümlerini depolar. Çerçeve, ikinci geçişi layout(int, int, int, int) içinde gerçekleştirir ve bu geçiş de yukarıdan aşağıya doğrudur. Bu geçiş sırasında her üst öğe, ölçü geçişinde hesaplanan boyutları kullanarak tüm alt öğelerini konumlandırmaktan sorumludur.
Düzenleme sürecinin iki geçişi, aşağıdaki bölümlerde daha ayrıntılı olarak açıklanmıştır.
Ölçüm geçişi başlatma
Bir View nesnesinin measure() yöntemi döndürüldüğünde, getMeasuredWidth() ve getMeasuredHeight() değerlerini View nesnesinin tüm alt öğelerinin değerleriyle birlikte ayarlayın. Bir View
nesnenin ölçülen genişlik ve ölçülen yükseklik değerleri, View nesnenin üst öğeleri tarafından uygulanan kısıtlamalara uymalıdır. Bu sayede, ölçüm kartı sona erdiğinde tüm ebeveynlerin çocuklarının tüm ölçümlerini kabul etmesi sağlanır.
Bir üst öğe View, alt öğelerini measure() birden fazla kez çağırabilir. Örneğin, ebeveynler tercih ettikleri boyutları belirlemek için çocukları bir kez belirtilmemiş boyutlarla ölçebilir. Alt öğelerin sınırsız boyutlarının toplamı çok büyük veya çok küçükse üst öğe, alt öğelerin boyutlarını sınırlayan değerlerle measure() işlevini tekrar çağırabilir.
Ölçü kartı, boyutları iletmek için iki sınıf kullanır. ViewGroup.LayoutParams
View sınıfı, nesnelerin tercih ettikleri boyutları ve konumları nasıl bildirdiklerini gösterir. Temel
ViewGroup.LayoutParams sınıfı, öğenin tercih edilen genişliğini ve yüksekliğini
View açıklar. Her boyut için aşağıdakilerden biri belirtilebilir:
- Tam boyut.
MATCH_PARENT, yaniViewiçin tercih edilen boyut, üst öğesinin boyutundan iç boşluk çıkarılarak elde edilen boyuttur.WRAP_CONTENT,Viewiçin tercih edilen boyut, içeriğini ve iç boşluğunu kapsayacak kadar büyük olmalıdır.
Farklı ViewGroup.LayoutParams alt sınıfları için farklı ViewGroup alt sınıfları vardır. Örneğin, RelativeLayout, alt View nesnelerini yatay ve dikey olarak ortalama özelliğini içeren kendi ViewGroup.LayoutParams alt sınıfına sahiptir.
MeasureSpec nesneleri, üst öğeden alt öğeye doğru ağaçta gereksinimleri aşağıya aktarmak için kullanılır. Bir MeasureSpec üç moddan birinde olabilir:
UNSPECIFIED: Bu, ebeveyn tarafından çocuğun hedef boyutunuViewbelirlemek için kullanılır. Örneğin,LinearLayout,UNSPECIFIEDyüksekliğe veEXACTLY240 genişliğe sahip alt öğesinimeasure()çağırarak 240 piksel genişlikteViewalt öğesinin ne kadar uzun olmak istediğini öğrenebilir.EXACTLY: Üst öğe, alt öğeye tam bir boyut uygulamak için bunu kullanır. Alt yayıncı bu boyutu kullanmalı ve tüm alt öğelerinin bu boyuta uygun olduğunu garanti etmelidir.AT MOST: Ebeveyn, çocuğa maksimum boyut sınırlaması uygulamak için bu özelliği kullanır. Çocuk, kendisinin ve tüm soyunun bu boyuta uygun olduğunu garanti etmelidir.
Düzen geçişi başlatma
Bir düzen başlatmak için
requestLayout() çağrısını yapın. Bu yöntem genellikle kendi sınırları içinde artık yer alamayacağını düşündüğünde View tarafından kendisi üzerinde çağrılır.
Özel ölçüm ve düzen mantığı uygulama
Özel bir ölçüm veya düzen mantığı uygulamak istiyorsanız mantığın uygulandığı yöntemleri geçersiz kılın:
onMeasure(int, int)
ve
onLayout(boolean, int, int, int, int).
Bu yöntemler sırasıyla measure(int, int) ve layout(int, int, int, int) tarafından çağrılır. measure(int, int) veya layout(int, int) yöntemlerini geçersiz kılmaya çalışmayın. Bu yöntemlerin her ikisi de final olduğundan geçersiz kılınamaz.
Aşağıdaki örnekte, WindowManager örnek uygulamasındaki `SplitLayout` sınıfında bu işlemin nasıl yapılacağı gösterilmektedir. SplitLayout iki veya daha fazla alt görünüme sahipse ve ekranda katlama varsa iki alt görünümü katlamanın her iki tarafına yerleştirir. Aşağıdaki örnekte, ölçüm ve düzenin geçersiz kılınmasıyla ilgili bir kullanım alanı gösterilmektedir. Ancak üretim için bu davranışı istiyorsanız SlidingPaneLayout kullanın.
Kotlin
/** * 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 }
Java
/** * 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; } } }