Skip to content

Instantly share code, notes, and snippets.

@alexfu
Last active December 31, 2015 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexfu/7918808 to your computer and use it in GitHub Desktop.
Save alexfu/7918808 to your computer and use it in GitHub Desktop.
An attempt at reversing the stack ordering of a simple FrameLayout.
<com.alexfu.reverseframelayout.ReversedFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_purple"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"/>
</com.alexfu.reverseframelayout.ReversedFrameLayout>
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alexfu.reverseframelayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class ReversedFrameLayout extends ViewGroup {
public ReversedFrameLayout(Context context) {
super(context);
}
public ReversedFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ReversedFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
// Measurement will ultimately be computing these values.
int maxHeight = 0;
int maxWidth = 0;
int childState = 0;
// Iterate through all children, measuring them and computing our dimensions
// from their size.
for (int i = count-1; i > -1; i--) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
// Measure the child.
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// Update our size information based on the layout params. Children
// that asked to be positioned on the left or right go in those gutters.
maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
childState = combineMeasuredStates(childState, child.getMeasuredState());
}
}
// Check against our minimum height and width
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
// Report our final dimensions.
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
resolveSizeAndState(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final int count = getChildCount();
for(int i = count-1; i > -1; i--) {
final View child = getChildAt(i);
final int width = child.getMeasuredWidth();
final int height = child.getMeasuredHeight();
if(child.getVisibility() != GONE) {
child.layout(left, top, left + width, top + height);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment