gpt4 book ai didi

android - 默认情况下调整内容 Opened Navigation Drawer

转载 作者:搜寻专家 更新时间:2023-11-01 09:28:39 24 4
gpt4 key购买 nike

我的左右各有两个抽屉。此时右抽屉处于锁闭状态,但左抽屉可以打开和关闭。打开抽屉时,我使用抽屉偏移量推送中间内容。现在我也希望右抽屉(默认打开)也应该移动/调整内容,但我做不到。因为当左边的抽屉打开时,它会将内容推到右边的抽屉下面。所以我想要在默认打开时右抽屉应该调整内容,当我打开左抽屉内容应该调整并且不应该移动到右抽屉下面。

这是我的代码:

<DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">

<RelativeLayout
android:id="@+id/main_screen"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/bg">

<include
android:id="@+id/includedView"
layout="@layout/view" />

</RelativeLayout>


<!-- ======= side menu ====== -->
<RelativeLayout

android:id="@+id/leftMenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left">

<include
layout="@layout/leftmenuLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" />

</RelativeLayout>

<RelativeLayout

android:id="@+id/rightMenu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right">

<include
layout="@layout/rightmenuLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent" />

</RelativeLayout>
<!-- ======================= -->
</DrawerLayout>

抽屉 Activity :

 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setScrimColor(Color.parseColor("#00000000"));


main_screen = (RelativeLayout) findViewById(R.id.main_screen);


leftMenu= (RelativeLayout) findViewById(R.id.leftMenu);

rightMenu= (RelativeLayout)
findViewById(R.id.rightMenu);

Display display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);

mDrawerLayout.addDrawerListener(new CustomDrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
float moveFactor = 0;
moveFactor = (drawerView.getWidth() * slideOffset);
main_screen.setTranslationX(moveFactor);

}

@Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}

@Override
public void onDrawerClosed(View drawerView) {
invalidateOptionsMenu();
}

@Override
public void onDrawerStateChanged(int newState) {

}
});

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, rightMenu);
mDrawerLayout.openDrawer(GravityCompat.END);


mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, leftMenu);


ViewGroup.LayoutParams params = leftMenu.getLayoutParams();
params.width = (((size.x / 2) / 4));
rl_slider_menu.setLayoutParams(params);

ViewGroup.LayoutParams paramsRight =
rightMenu.getLayoutParams();
paramsRight.width = ((size.x / 3));
rightMenu.setLayoutParams(paramsRight);

DrawerLayout.LayoutParams paramsContent = (DrawerLayout.LayoutParams) main_screen.getLayoutParams();
paramsContent.setMargins(0, 0, (((size.x / 2) / 4)), 0); //substitute parameters for left, top, right, bottom
main_screen.setLayoutParams(paramsContent);
main_screen.invalidate();

最佳答案

这实际上证明是一个非常简单的实现,一旦您全神贯注。

我们只需要跟踪两个抽屉的 inset 尺寸,将内容 View 缩放到剩余空间,然后将其平移每个 inset 的一半差值。例如。以问题中的代码为基础:

mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
private float leftInset, rightInset;

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
if (drawerView == leftMenu) {
leftInset = slideOffset * leftMenu.getWidth();
}
else {
rightInset = slideOffset * rightMenu.getWidth();
}

final float scale = 1 - (leftInset + rightInset) / main_screen.getWidth();
main_screen.setScaleX(scale);

final float translation = leftInset / 2 - rightInset / 2;
main_screen.setTranslationX(translation);
}

...
}

关于android - 默认情况下调整内容 Opened Navigation Drawer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49067249/

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