From 618c37e8376c8396ea2b45d1ac21357dad944e50 Mon Sep 17 00:00:00 2001 From: Jorge Aguilar Date: Mon, 27 Mar 2017 15:06:51 -0700 Subject: [PATCH] Fix for shadow (#716) * - Removing the view for API 21 - Adding a negative margin for API 20 or less so that the content is overlapped with the shadow - Fix the annotation for the MiscUtils * - Removing the view for API 21 - Adding a negative margin for API 20 or less so that the content is overlapped with the shadow - Fix the annotation for the MiscUtils * - Removing the view for API 21 - Adding a negative margin for API 20 or less so that the content is overlapped with the shadow - Fix the annotation for the MiscUtils --- .../com/roughike/bottombar/BottomBar.java | 72 ++++++++++++++----- .../com/roughike/bottombar/MiscUtils.java | 4 +- .../bb_bottom_bar_item_container.xml | 24 +++++++ .../layout/bb_bottom_bar_item_container.xml | 6 +- bottom-bar/src/main/res/values/dimensions.xml | 2 + 5 files changed, 85 insertions(+), 23 deletions(-) create mode 100644 bottom-bar/src/main/res/layout-v21/bb_bottom_bar_item_container.xml diff --git a/bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java b/bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java index ff90c7a3..f38599af 100644 --- a/bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java +++ b/bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java @@ -16,6 +16,7 @@ import android.support.annotation.IdRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.RequiresApi; import android.support.annotation.VisibleForTesting; import android.support.annotation.XmlRes; import android.support.design.widget.CoordinatorLayout; @@ -26,6 +27,7 @@ import android.view.View; import android.view.ViewAnimationUtils; import android.view.ViewGroup; +import android.view.ViewOutlineProvider; import android.view.ViewParent; import android.widget.LinearLayout; import android.widget.TextView; @@ -76,11 +78,11 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie private int titleTextAppearance; private Typeface titleTypeFace; private boolean showShadow; + private float shadowElevation; private View backgroundOverlay; private ViewGroup outerContainer; private ViewGroup tabContainer; - private View shadowView; private int defaultBackgroundColor = Color.WHITE; private int currentBackgroundColor; @@ -107,35 +109,77 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie private BottomBarTab[] currentTabs; public BottomBar(Context context) { - super(context); - init(context, null); + this(context, null); } public BottomBar(Context context, AttributeSet attrs) { - super(context, attrs); - init(context, attrs); + this(context, attrs, 0); } - private void init(Context context, AttributeSet attrs) { + public BottomBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(context, attrs, defStyleAttr, 0); + } + + @RequiresApi(Build.VERSION_CODES.LOLLIPOP) + public BottomBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + init(context, attrs, defStyleAttr, defStyleRes); + } + + private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { batchPropertyApplier = new BatchTabPropertyApplier(this); - populateAttributes(context, attrs); + populateAttributes(context, attrs, defStyleAttr, defStyleRes); initializeViews(); determineInitialBackgroundColor(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + init21(context); + } + if (tabXmlResource != 0) { setItems(tabXmlResource); } } - private void populateAttributes(Context context, AttributeSet attrs) { + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + + // This is so that in Pre-Lollipop devices there is a shadow BUT without pushing the content + if (showShadow) { + ViewGroup.LayoutParams params = getLayoutParams(); + if (params instanceof MarginLayoutParams) { + MarginLayoutParams layoutParams = (MarginLayoutParams) params; + final int shadowHeight = getResources().getDimensionPixelSize(R.dimen.bb_fake_shadow_height); + + layoutParams.setMargins(layoutParams.leftMargin, + layoutParams.topMargin - shadowHeight, + layoutParams.rightMargin, + layoutParams.bottomMargin); + setLayoutParams(params); + } + } + } + + @RequiresApi(Build.VERSION_CODES.LOLLIPOP) + private void init21(Context context) { + shadowElevation = getElevation(); + shadowElevation = shadowElevation > 0 + ? shadowElevation + : getResources().getDimensionPixelSize(R.dimen.bb_default_elevation); + setElevation(MiscUtils.dpToPixel(context, shadowElevation)); + setOutlineProvider(ViewOutlineProvider.BOUNDS); + } + + private void populateAttributes(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { primaryColor = MiscUtils.getColor(getContext(), R.attr.colorPrimary); screenWidth = MiscUtils.getScreenWidth(getContext()); tenDp = MiscUtils.dpToPixel(getContext(), 10); maxFixedItemWidth = MiscUtils.dpToPixel(getContext(), 168); - TypedArray ta = context.getTheme().obtainStyledAttributes( - attrs, R.styleable.BottomBar, 0, 0); + TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BottomBar, defStyleAttr, defStyleRes); try { tabXmlResource = ta.getResourceId(R.styleable.BottomBar_bb_tabXmlResource, 0); @@ -196,7 +240,6 @@ private void initializeViews() { setLayoutParams(params); setOrientation(isTabletMode ? HORIZONTAL : VERTICAL); - ViewCompat.setElevation(this, MiscUtils.dpToPixel(getContext(), 8)); View rootView = inflate(getContext(), isTabletMode ? R.layout.bb_bottom_bar_item_container_tablet : R.layout.bb_bottom_bar_item_container, this); @@ -205,11 +248,6 @@ private void initializeViews() { backgroundOverlay = rootView.findViewById(R.id.bb_bottom_bar_background_overlay); outerContainer = (ViewGroup) rootView.findViewById(R.id.bb_bottom_bar_outer_container); tabContainer = (ViewGroup) rootView.findViewById(R.id.bb_bottom_bar_item_container); - shadowView = rootView.findViewById(R.id.bb_bottom_bar_shadow); - - if (!showShadow) { - shadowView.setVisibility(GONE); - } } private void determineInitialBackgroundColor() { @@ -351,8 +389,6 @@ private void resizeTabsToCorrectSizes(BottomBarTab[] tabsToAdd) { if (tabView.getParent() == null) { tabContainer.addView(tabView); } - - tabView.requestLayout(); } } diff --git a/bottom-bar/src/main/java/com/roughike/bottombar/MiscUtils.java b/bottom-bar/src/main/java/com/roughike/bottombar/MiscUtils.java index 7904ab61..a0635b6b 100644 --- a/bottom-bar/src/main/java/com/roughike/bottombar/MiscUtils.java +++ b/bottom-bar/src/main/java/com/roughike/bottombar/MiscUtils.java @@ -4,8 +4,8 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; +import android.support.annotation.AttrRes; import android.support.annotation.ColorInt; -import android.support.annotation.ColorRes; import android.support.annotation.Dimension; import android.support.annotation.NonNull; import android.support.annotation.Px; @@ -35,7 +35,7 @@ class MiscUtils { @ColorInt - protected static int getColor(@NonNull Context context, @ColorRes int color) { + protected static int getColor(@NonNull Context context, @AttrRes int color) { TypedValue tv = new TypedValue(); context.getTheme().resolveAttribute(color, tv, true); return tv.data; diff --git a/bottom-bar/src/main/res/layout-v21/bb_bottom_bar_item_container.xml b/bottom-bar/src/main/res/layout-v21/bb_bottom_bar_item_container.xml new file mode 100644 index 00000000..1185bd5f --- /dev/null +++ b/bottom-bar/src/main/res/layout-v21/bb_bottom_bar_item_container.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/bottom-bar/src/main/res/layout/bb_bottom_bar_item_container.xml b/bottom-bar/src/main/res/layout/bb_bottom_bar_item_container.xml index fa763fd9..ef8f7b6e 100644 --- a/bottom-bar/src/main/res/layout/bb_bottom_bar_item_container.xml +++ b/bottom-bar/src/main/res/layout/bb_bottom_bar_item_container.xml @@ -1,11 +1,11 @@ - + android:layout_height="@dimen/bb_fake_shadow_height" + android:background="@drawable/bb_bottom_bar_top_shadow" /> 56dp + 8dp + 4dp \ No newline at end of file