gpt4 book ai didi

带有图标的 Android 抽屉始终可见

转载 作者:行者123 更新时间:2023-11-29 00:05:58 25 4
gpt4 key购买 nike

哪个布局组件最适合构建带有始终可见的图标的抽屉菜单?我不想使用 SlidingPaneLayout,因为主要内容不应受此影响。

enter image description here

最佳答案

1. 添加CrossFadeSlidingPaneLayout

  public class CrossFadeSlidingPaneLayout extends SlidingPaneLayout {
private View partialView = null;
private View fullView = null;

// helper flag pre honeycomb used in visibility and click response handling
// helps avoid unnecessary layouts
private boolean wasOpened = false;

public CrossFadeSlidingPaneLayout(Context context) {
super(context);
}

public CrossFadeSlidingPaneLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public CrossFadeSlidingPaneLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();

if (getChildCount() < 1) {
return;
}

View panel = getChildAt(0);
if (!(panel instanceof ViewGroup)) {
return;
}

ViewGroup viewGroup = (ViewGroup) panel;
if (viewGroup.getChildCount() != 2) {
return;
}
fullView = viewGroup.getChildAt(0);
partialView = viewGroup.getChildAt(1);

super.setPanelSlideListener(crossFadeListener);
}

@Override
public void setPanelSlideListener(final PanelSlideListener listener) {
if (listener == null) {
super.setPanelSlideListener(crossFadeListener);
return;
}

super.setPanelSlideListener(new PanelSlideListener() {
@Override
public void onPanelSlide(View panel, float slideOffset) {
crossFadeListener.onPanelSlide(panel, slideOffset);
listener.onPanelSlide(panel, slideOffset);
}

@Override
public void onPanelOpened(View panel) {
listener.onPanelOpened(panel);
}

@Override
public void onPanelClosed(View panel) {
listener.onPanelClosed(panel);
}
});
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);

if (partialView != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
// "changed" means that views were added or removed
// we need to move the partial view out of the way in any case (if it's supposed to of course)
updatePartialViewVisibilityPreHoneycomb(isOpen());
} else {
partialView.setVisibility(isOpen() ? View.GONE : VISIBLE);
}
}
}

private SimplePanelSlideListener crossFadeListener = new SimplePanelSlideListener() {
@Override
public void onPanelSlide(View panel, float slideOffset) {
super.onPanelSlide(panel, slideOffset);
if (partialView == null || fullView == null) {
return;
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
if (slideOffset == 1 && !wasOpened) {
// the layout was just opened, move the partial view off screen
updatePartialViewVisibilityPreHoneycomb(true);
wasOpened = true;
} else if (slideOffset < 1 && wasOpened) {
// the layout just started to close, move the partial view back, so it can be shown animating
updatePartialViewVisibilityPreHoneycomb(false);
wasOpened = false;
}
} else {
partialView.setVisibility(isOpen() ? View.GONE : VISIBLE);
}

ViewHelper.setAlpha(partialView, 1 - slideOffset);
ViewHelper.setAlpha(fullView, slideOffset);
}
};

private void updatePartialViewVisibilityPreHoneycomb(boolean slidingPaneOpened) {
// below API 11 the top view must be moved so it does not consume clicks intended for the bottom view
// this applies curiously even when setting its visibility to GONE
// this might be due to platform limitations or it may have been introduced by NineOldAndroids library
if (slidingPaneOpened) {
partialView.layout(-partialView.getWidth(), 0, 0, partialView.getHeight());
} else {
partialView.layout(0, 0, partialView.getWidth(), partialView.getHeight());
}
}
}

2. Activity 布局

    <com.xxx.CrossFadeSlidingPaneLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sliding_pane_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:layout_width="@dimen/side_pane_width"
android:layout_height="match_parent"
android:background="@color/purple">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="7dp"
android:background="@drawable/ic_launcher" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Your Screen" />
</LinearLayout>


<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="7dp"
android:background="@drawable/ic_launcher" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Your Screen" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="7dp"
android:background="@drawable/ic_launcher" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Your Screen" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="55dp"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_launcher" />

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_launcher" />

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_launcher" />
</LinearLayout>

</FrameLayout>

<TextView
android:layout_width="@dimen/main_pane_width"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/partial_pane_width"
android:layout_weight="1"
android:background="@color/light_blue"
android:text="@string/pane_2" />
</com.xxx.CrossFadeSlidingPaneLayout>

现在已经完成,菜单总是可见的:)

归因于原始项目:Github Url

关于带有图标的 Android 抽屉始终可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33768323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com