PreferenceFrameLayout.java revision 8b2fb60cd49426c77869ddd91cbf4130d337a41c
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.preference; 18 19import android.content.Context; 20import android.content.res.TypedArray; 21import android.util.AttributeSet; 22import android.view.View; 23import android.view.ViewGroup.MarginLayoutParams; 24import android.widget.FrameLayout; 25import android.widget.FrameLayout.LayoutParams; 26 27/** 28 * @hide 29 */ 30public class PreferenceFrameLayout extends FrameLayout { 31 private static final int DEFAULT_BORDER_TOP = 0; 32 private static final int DEFAULT_BORDER_BOTTOM = 0; 33 private static final int DEFAULT_BORDER_LEFT = 0; 34 private static final int DEFAULT_BORDER_RIGHT = 0; 35 private final int mBorderTop; 36 private final int mBorderBottom; 37 private final int mBorderLeft; 38 private final int mBorderRight; 39 private boolean mPaddingApplied = false; 40 41 public PreferenceFrameLayout(Context context) { 42 this(context, null); 43 } 44 45 public PreferenceFrameLayout(Context context, AttributeSet attrs) { 46 this(context, attrs, com.android.internal.R.attr.preferenceFrameLayoutStyle); 47 } 48 49 public PreferenceFrameLayout(Context context, AttributeSet attrs, int defStyle) { 50 super(context, attrs, defStyle); 51 TypedArray a = context.obtainStyledAttributes(attrs, 52 com.android.internal.R.styleable.PreferenceFrameLayout, defStyle, 0); 53 54 float density = context.getResources().getDisplayMetrics().density; 55 int defaultBorderTop = (int) (density * DEFAULT_BORDER_TOP + 0.5f); 56 int defaultBottomPadding = (int) (density * DEFAULT_BORDER_BOTTOM + 0.5f); 57 int defaultLeftPadding = (int) (density * DEFAULT_BORDER_LEFT + 0.5f); 58 int defaultRightPadding = (int) (density * DEFAULT_BORDER_RIGHT + 0.5f); 59 60 mBorderTop = a.getDimensionPixelSize( 61 com.android.internal.R.styleable.PreferenceFrameLayout_borderTop, 62 defaultBorderTop); 63 mBorderBottom = a.getDimensionPixelSize( 64 com.android.internal.R.styleable.PreferenceFrameLayout_borderBottom, 65 defaultBottomPadding); 66 mBorderLeft = a.getDimensionPixelSize( 67 com.android.internal.R.styleable.PreferenceFrameLayout_borderLeft, 68 defaultLeftPadding); 69 mBorderRight = a.getDimensionPixelSize( 70 com.android.internal.R.styleable.PreferenceFrameLayout_borderRight, 71 defaultRightPadding); 72 73 74 a.recycle(); 75 } 76 77 /** 78 * {@inheritDoc} 79 */ 80 @Override 81 public LayoutParams generateLayoutParams(AttributeSet attrs) { 82 return new LayoutParams(getContext(), attrs); 83 } 84 85 @Override 86 public void addView(View child) { 87 int borderTop = getPaddingTop(); 88 int borderBottom = getPaddingBottom(); 89 int borderLeft = getPaddingLeft(); 90 int borderRight = getPaddingRight(); 91 92 LayoutParams layoutParams = (PreferenceFrameLayout.LayoutParams) child.getLayoutParams(); 93 // Check on the id of the child before adding it. 94 if (layoutParams != null && layoutParams.removeBorders) { 95 if (mPaddingApplied) { 96 borderTop -= mBorderTop; 97 borderBottom -= mBorderBottom; 98 borderLeft -= mBorderLeft; 99 borderRight -= mBorderRight; 100 mPaddingApplied = false; 101 } 102 } else { 103 // Add the padding to the view group after determining if the 104 // padding already exists. 105 if (!mPaddingApplied) { 106 borderTop += mBorderTop; 107 borderBottom += mBorderBottom; 108 borderLeft += mBorderLeft; 109 borderRight += mBorderRight; 110 mPaddingApplied = true; 111 } 112 } 113 114 int previousTop = getPaddingTop(); 115 int previousBottom = getPaddingBottom(); 116 int previousLeft = getPaddingLeft(); 117 int previousRight = getPaddingRight(); 118 if (previousTop != borderTop || previousBottom != borderBottom 119 || previousLeft != borderLeft || previousRight != borderRight) { 120 setPadding(borderLeft, borderTop, borderRight, borderBottom); 121 } 122 123 super.addView(child); 124 } 125 126 public static class LayoutParams extends FrameLayout.LayoutParams { 127 public boolean removeBorders = false; 128 /** 129 * {@inheritDoc} 130 */ 131 public LayoutParams(Context c, AttributeSet attrs) { 132 super(c, attrs); 133 134 TypedArray a = c.obtainStyledAttributes(attrs, 135 com.android.internal.R.styleable.PreferenceFrameLayout_Layout); 136 removeBorders = a.getBoolean( 137 com.android.internal.R.styleable.PreferenceFrameLayout_Layout_layout_removeBorders, 138 false); 139 a.recycle(); 140 } 141 142 /** 143 * {@inheritDoc} 144 */ 145 public LayoutParams(int width, int height) { 146 super(width, height); 147 } 148 } 149}