PreferenceFrameLayout.java revision 617feb99a06e7ffb3894e86a286bf30e085f321a
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.app.FragmentBreadCrumbs; 20import android.content.Context; 21import android.content.res.TypedArray; 22import android.util.AttributeSet; 23import android.view.View; 24import android.widget.FrameLayout; 25 26/** 27 * @hide 28 */ 29public class PreferenceFrameLayout extends FrameLayout { 30 private static final int DEFAULT_BORDER_TOP = 0; 31 private static final int DEFAULT_BORDER_BOTTOM = 0; 32 private static final int DEFAULT_BORDER_LEFT = 0; 33 private static final int DEFAULT_BORDER_RIGHT = 0; 34 private final int mBorderTop; 35 private final int mBorderBottom; 36 private final int mBorderLeft; 37 private final int mBorderRight; 38 private boolean mPaddingApplied; 39 40 public PreferenceFrameLayout(Context context) { 41 this(context, null); 42 } 43 44 public PreferenceFrameLayout(Context context, AttributeSet attrs) { 45 this(context, attrs, com.android.internal.R.attr.preferenceFrameLayoutStyle); 46 } 47 48 public PreferenceFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { 49 this(context, attrs, defStyleAttr, 0); 50 } 51 52 public PreferenceFrameLayout( 53 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 54 super(context, attrs, defStyleAttr, defStyleRes); 55 final TypedArray a = context.obtainStyledAttributes(attrs, 56 com.android.internal.R.styleable.PreferenceFrameLayout, defStyleAttr, defStyleRes); 57 58 float density = context.getResources().getDisplayMetrics().density; 59 int defaultBorderTop = (int) (density * DEFAULT_BORDER_TOP + 0.5f); 60 int defaultBottomPadding = (int) (density * DEFAULT_BORDER_BOTTOM + 0.5f); 61 int defaultLeftPadding = (int) (density * DEFAULT_BORDER_LEFT + 0.5f); 62 int defaultRightPadding = (int) (density * DEFAULT_BORDER_RIGHT + 0.5f); 63 64 mBorderTop = a.getDimensionPixelSize( 65 com.android.internal.R.styleable.PreferenceFrameLayout_borderTop, 66 defaultBorderTop); 67 mBorderBottom = a.getDimensionPixelSize( 68 com.android.internal.R.styleable.PreferenceFrameLayout_borderBottom, 69 defaultBottomPadding); 70 mBorderLeft = a.getDimensionPixelSize( 71 com.android.internal.R.styleable.PreferenceFrameLayout_borderLeft, 72 defaultLeftPadding); 73 mBorderRight = a.getDimensionPixelSize( 74 com.android.internal.R.styleable.PreferenceFrameLayout_borderRight, 75 defaultRightPadding); 76 77 a.recycle(); 78 } 79 80 /** 81 * {@inheritDoc} 82 */ 83 @Override 84 public LayoutParams generateLayoutParams(AttributeSet attrs) { 85 return new LayoutParams(getContext(), attrs); 86 } 87 88 @Override 89 public void addView(View child) { 90 int borderTop = getPaddingTop(); 91 int borderBottom = getPaddingBottom(); 92 int borderLeft = getPaddingLeft(); 93 int borderRight = getPaddingRight(); 94 95 android.view.ViewGroup.LayoutParams params = child.getLayoutParams(); 96 LayoutParams layoutParams = params instanceof PreferenceFrameLayout.LayoutParams 97 ? (PreferenceFrameLayout.LayoutParams) child.getLayoutParams() : null; 98 // Check on the id of the child before adding it. 99 if (layoutParams != null && layoutParams.removeBorders) { 100 if (mPaddingApplied) { 101 borderTop -= mBorderTop; 102 borderBottom -= mBorderBottom; 103 borderLeft -= mBorderLeft; 104 borderRight -= mBorderRight; 105 mPaddingApplied = false; 106 } 107 } else { 108 // Add the padding to the view group after determining if the 109 // padding already exists. 110 if (!mPaddingApplied) { 111 borderTop += mBorderTop; 112 borderBottom += mBorderBottom; 113 borderLeft += mBorderLeft; 114 borderRight += mBorderRight; 115 mPaddingApplied = true; 116 } 117 } 118 119 int previousTop = getPaddingTop(); 120 int previousBottom = getPaddingBottom(); 121 int previousLeft = getPaddingLeft(); 122 int previousRight = getPaddingRight(); 123 if (previousTop != borderTop || previousBottom != borderBottom 124 || previousLeft != borderLeft || previousRight != borderRight) { 125 setPadding(borderLeft, borderTop, borderRight, borderBottom); 126 } 127 128 super.addView(child); 129 } 130 131 public static class LayoutParams extends FrameLayout.LayoutParams { 132 public boolean removeBorders = false; 133 /** 134 * {@inheritDoc} 135 */ 136 public LayoutParams(Context c, AttributeSet attrs) { 137 super(c, attrs); 138 139 TypedArray a = c.obtainStyledAttributes(attrs, 140 com.android.internal.R.styleable.PreferenceFrameLayout_Layout); 141 removeBorders = a.getBoolean( 142 com.android.internal.R.styleable.PreferenceFrameLayout_Layout_layout_removeBorders, 143 false); 144 a.recycle(); 145 } 146 147 /** 148 * {@inheritDoc} 149 */ 150 public LayoutParams(int width, int height) { 151 super(width, height); 152 } 153 } 154}