我使用https://github.com/umano/AndroidSlidingUpPanel实现了UmanoSlidingPanel。一切都工作得很好,除了我的滑动面板扩展时,滑动内容(sliding_view)低于状态栏。我怎么能避免这种情况?我试着删除
<item name="android:windowTranslucentStatus">true</item>
来自styles.xml的但是这也使主要内容的状态栏不是半透明的。有什么方法可以解决这个问题吗?
<com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:umanoDragView="@+id/firstPanelDragArea"
sothree:umanoPanelHeight="@dimen/bottom_panel_height"
sothree:umanoShadowHeight="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- SLIDING LAYOUT -->
<LinearLayout
android:id="@+id/firstPanelDragArea"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/sliding_view" />
</LinearLayout>
styles.xml
<style name="AppTheme" parent="@style/Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="windowNoTitle">true</item>
</style>
修改
public void onPanelSlide(View panel, float slideOffset) {
if (slideOffset > 0.5 && !flagTransparent) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
flagTransparent = true;
Log.i(TAG, "onPanelSlide, offset " + slideOffset);
} else if (slideOffset < 0.5 && flagTransparent){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
flagTransparent = false;
Log.i(TAG, "onPanelSlide, offset " + slideOffset);
}
答案 0 :(得分:1)
1)看看我创建的以下Gist。
文件sliding_up_activity.xml
可以是包含向上滑动面板视图的活动布局。
在那里你应该设置android:fitsSystemWindows="true"
,以便任何孩子都能正确插入,而不是落在操作栏或导航栏后面。
文件sliding_view.xml
可能是您的向上滑动视图。
您不需要在任何面板回调中执行任何其他操作。
您可以安全地删除onPanelSlide()
内的半透明样式和代码。这应该导致滑动面板正确插入,以便永远不会落后于状态或导航栏。
或者,如果您没有相同的架构,则可以忘记sliding_up_activity.xml
并在android:fitsSystemWindows="true"
中设置CoordinatorLayout
,而不是sliding_view.xml
。
2)我们谈论的第二件事是使状态栏保持半透明状态,让滑动面板落在状态栏后面,然后平滑地添加填充以正确插入内容。
为此,请设置android:fitsSystemWindows="false"
或将其从您的向上滑动面板的任何可能的父级中移除。
然后在onPanelSlide
你可以做这样的事情
if (slideOffset >= ANCHOR_POINT) {
// adjust the span of the offset
float offset = (slideOffset - ANCHOR_POINT) / (1 - ANCHOR_POINT);
// the padding top will be the base original padding plus a percentage of the status
// bar height
float paddingTop = mSlidePanelBasePadding + offset * mStatusBarInset;
// apply the padding to the header container
mContainer.setPadding(0, (int) paddingTop, 0, 0);
}
其中:
mSlidePanelBasePadding
将是您向上滑动面板的初始填充mStatusBarInset
将是状态栏高度。当它越过锚点时,应该逐步向slideUp面板添加填充。
您可以像这样获取状态栏高度:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
我希望这有点帮助。
答案 1 :(得分:1)
通过在SlidingUpPanelLayout.java的onMeasure方法中进行更改,找到了另一个问题的解决方案。为未来的读者发布代码。需要添加4行代码。
SlidingPanelLayout.java
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("Width must have an exact value or MATCH_PARENT");
} else if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("Height must have an exact value or MATCH_PARENT");
}
final int childCount = getChildCount();
if (childCount != 2) {
throw new IllegalStateException("Sliding up panel layout must have exactly 2 children!");
}
mMainView = getChildAt(0);
mSlideableView = getChildAt(1);
if (mDragView == null) {
setDragView(mSlideableView);
}
// If the sliding panel is not visible, then put the whole view in the hidden state
if (mSlideableView.getVisibility() != VISIBLE) {
mSlideState = PanelState.HIDDEN;
}
int layoutHeight = heightSize - getPaddingTop() - getPaddingBottom();
int layoutWidth = widthSize - getPaddingLeft() - getPaddingRight();
// First pass. Measure based on child LayoutParams width/height.
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
// We always measure the sliding panel in order to know it's height (needed for show panel)
if (child.getVisibility() == GONE && i == 0) {
continue;
}
int height = layoutHeight;
int width = layoutWidth;
if (child == mMainView) {
if (!mOverlayContent && mSlideState != PanelState.HIDDEN) {
height -= mPanelHeight;
}
width -= lp.leftMargin + lp.rightMargin;
} else if (child == mSlideableView) {
// The slideable view should be aware of its top margin.
// See https://github.com/umano/AndroidSlidingUpPanel/issues/412.
// height -= lp.topMargin;.
// START OF CODE CHANGES
if (height < SCREEN_HEIGHT) { //SCREEN_HEIGHT = 900
height = height - MARGIN_BOTTOM; //MARGIN_BOTTOM = 8
} else {
height = height - getStatusBarHeight();
}
// END OF CODE CHANGES
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
答案 2 :(得分:0)
在父版式中尝试android:fitsSystemWindows="true"
。这应该可以解决你的问题。
我过去实现的另一个解决方案是,在包含slidingUpPanel的活动中,您可以使用半透明状态栏(API&gt; = 19)对其进行样式设置:
<item name="android:windowTranslucentStatus">true</item>
然后在onPanelSlide
回调中,您可以修改slideUpPanel的填充,逐步添加,直到面板完全启动。在那一刻,添加的额外填充量应该是状态栏尺寸。