PreferenceFrameLayout.java revision c57406cdfa1925c3474e87865c51950b76ee0347
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 android.view.ViewGroup.LayoutParams params = child.getLayoutParams(); 93 LayoutParams layoutParams = params instanceof PreferenceFrameLayout.LayoutParams 94 ? (PreferenceFrameLayout.LayoutParams) child.getLayoutParams() : null; 95 // Check on the id of the child before adding it. 96 if (layoutParams != null && layoutParams.removeBorders) { 97 if (mPaddingApplied) { 98 borderTop -= mBorderTop; 99 borderBottom -= mBorderBottom; 100 borderLeft -= mBorderLeft; 101 borderRight -= mBorderRight; 102 mPaddingApplied = false; 103 } 104 } else { 105 // Add the padding to the view group after determining if the 106 // padding already exists. 107 if (!mPaddingApplied) { 108 borderTop += mBorderTop; 109 borderBottom += mBorderBottom; 110 borderLeft += mBorderLeft; 111 borderRight += mBorderRight; 112 mPaddingApplied = true; 113 } 114 } 115 116 int previousTop = getPaddingTop(); 117 int previousBottom = getPaddingBottom(); 118 int previousLeft = getPaddingLeft(); 119 int previousRight = getPaddingRight(); 120 if (previousTop != borderTop || previousBottom != borderBottom 121 || previousLeft != borderLeft || previousRight != borderRight) { 122 setPadding(borderLeft, borderTop, borderRight, borderBottom); 123 } 124 125 super.addView(child); 126 } 127 128 public static class LayoutParams extends FrameLayout.LayoutParams { 129 public boolean removeBorders = false; 130 /** 131 * {@inheritDoc} 132 */ 133 public LayoutParams(Context c, AttributeSet attrs) { 134 super(c, attrs); 135 136 TypedArray a = c.obtainStyledAttributes(attrs, 137 com.android.internal.R.styleable.PreferenceFrameLayout_Layout); 138 removeBorders = a.getBoolean( 139 com.android.internal.R.styleable.PreferenceFrameLayout_Layout_layout_removeBorders, 140 false); 141 a.recycle(); 142 } 143 144 /** 145 * {@inheritDoc} 146 */ 147 public LayoutParams(int width, int height) { 148 super(width, height); 149 } 150 } 151}