如何在android中处理窗口插入



如何正确处理android中的顶部和底部系统窗口插件?

有人能举个例子吗?

我在布局中使用了android:fitsSystemWindows=”true”,但它仍然不起作用。

如何让getSystemWindowInsetBottomgetSystemWindowInsetTop添加填充。

[示例]图像来自https://chris.banes.dev/2019/04/12/insets-listeners-to-layouts/【示例】:https://i.stack.imgur.com/OBdvN.png

我的代码`@BindView(R.id.slideViewPager(ViewPager ViewPager;

@BindView(R.id.layoutDots)
LinearLayout dotsLayout;
@BindView(R.id.btn_next)
Button btn_next;
private ImageView[] dots;
private int[] layouts;
private int mCurrentPage;
@Inject
PreferenceUtil preferenceUtil;
Animation animation;
int topPadding, bottomPadding;
View view;

//  @BindView(R.id.activity_walkthrough)
RelativeLayout relativeLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addTransparentStatusBar();
Injector.component().inject(this);
relativeLayout = (RelativeLayout) findViewById(R.id.activity_walkthrough);
setContentView(R.layout.activity_walkthrough);
ViewCompat.setOnApplyWindowInsetsListener(view, (OnApplyWindowInsetsListener) (v, insets) -> {
view = relativeLayout.getRootView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
topPadding = view.getRootWindowInsets().getSystemWindowInsetTop();
bottomPadding = view.getRootWindowInsets().getSystemWindowInsetBottom();
}
return null;
});

layouts = new int[]{
R.layout.fragment_walkthorugh1,
R.layout.fragment_walkthorugh2,
R.layout.fragment_walkthorugh3};
// adding bottom dots
addBottomDots(0);
setupViewPager();
animation = AnimationUtils.loadAnimation(this, R.anim.up);
btn_next.startAnimation(animation);
}
private void addBottomDots(int currentPage) {
if (dotsLayout != null) {
dotsLayout.removeAllViews();
}
dots = new ImageView[layouts.length];
for (int i = 0; i < dots.length; i++) {
dots[i] = new ImageView(this);
dots[i].setPadding(0, 0, 5, 0);
if (i == currentPage) {
dots[i].setImageDrawable(getResources().getDrawable(R.drawable.active_dots));
} else {
dots[i].setImageDrawable(getResources().getDrawable(R.drawable.default_dots));
}
dotsLayout.addView(dots[i]);
}
}
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
private void launchHomeScreen() {
preferenceUtil.setFirstTimeLaunch(false);
showBottomSheet();
// startActivity(LoginActivity.class,null);
// finish();
}
public void showBottomSheet() {
BottomSheetFragment addPhotoBottomDialogFragment = new BottomSheetFragment();
addPhotoBottomDialogFragment.show(getSupportFragmentManager(), BottomSheetFragment.TAG);
}
public void addTransparentStatusBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
private void setupViewPager() {
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), 3);
Walkthrough1 walkthrough1 = new Walkthrough1();
Walkthrough2 walkthrough2 = new Walkthrough2();
Walkthrough3 walkthrough3 = new Walkthrough3();
viewPagerAdapter.addFragment(walkthrough1);
viewPagerAdapter.addFragment(walkthrough2);
viewPagerAdapter.addFragment(walkthrough3);
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
addBottomDots(position);
mCurrentPage = position;
// changing the next button text 'NEXT' / 'GOT IT'
if (position == layouts.length - 1) {
btn_next.setText(getString(R.string.start));
} else {
btn_next.setText(getString(R.string.next));
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@OnClick(R.id.btn_next)
void nextPage() {
Toast.makeText(getApplicationContext(),"Top Padding : " + topPadding + " and bottom Padding " + bottomPadding,Toast.LENGTH_SHORT).show();
mCurrentPage = getItem(+1);
if (mCurrentPage < layouts.length) {
viewPager.setCurrentItem(mCurrentPage);
} else {
launchHomeScreen();
}
}

android:fitsSystemWindows="true"只适用于某些布局父级,而不是所有。。。它适用于Coordinator布局和Constraint布局。

您可以自己处理系统插入:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val topPadding = view.rootWindowInsets.systemWindowInsetTop
val bottomPadding = view.rootWindowInsets.systemWindowInsetBottom
}

但只有当视图附加到窗口时,您才能访问rootWindowInsets。在以下回调中获取rootWindowInserts是安全的:

view.addOnAttachStateChangeListener(...)

ViewCompat.setOnApplyWindowInsetsListener(...)

最新更新