android中的窗口插入

android中的窗口插入,android,Android,如何在android中正确处理顶部和底部系统窗口插入 有人能举个例子吗 我在布局中使用了android:fitsystemwindows=“true”,但仍然不起作用 如何获取getsystemwindowinsettom和getSystemWindowInsetTop以添加填充 来自 我的代码'@BindView(R.id.slideviewpage) 查看页面查看页面 @BindView(R.id.layoutDots) LinearLayout dotsLayout; @BindView

如何在android中正确处理顶部和底部系统窗口插入

有人能举个例子吗

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

如何获取
getsystemwindowinsettom
getSystemWindowInsetTop
以添加填充

来自 我的代码'@BindView(R.id.slideviewpage) 查看页面查看页面

@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();
    }
}
@bindwiew(R.id.layoutDots)
线性布局点slayout;
@BindView(R.id.btn\u下一步)
按钮btn_下一步;
私有图像视图[]点;
私人int[]布局;
私有int mCurrentPage;
@注入
PreferenceUtil PreferenceUtil;
动画;
int-topPadding,bottomPadding;
视图;
//@BindView(R.id.activity\u演练)
相对的相对的;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addTransparentStatusBar();
Injector.component().Injector(这个);
relativeLayout=(relativeLayout)findViewById(R.id.activity\u演练);
setContentView(R.layout.activity\u演练);
ViewCompat.SetOnApplyWindowInsertsListener(视图,(OnApplyWindowInsertsListener)(v,插图)->{
view=relativeLayout.getRootView();
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.KITKAT){
topPadding=view.getRootWindowInsetts().getSystemWindowInsetTop();
bottomPadding=view.getRootWindowInsets().getSystemWindowInsertBottom();
}
返回null;
});
布局=新int[]{
R.layout.fragment_walkthough1,
R.layout.fragment_walkthough2,
R.layout.fragment_walktorugh3};
//添加底点
添加点(0);
setupViewPager();
animation=AnimationUtils.loadAnimation(this,R.anim.up);
btn_next.开始动画(动画);
}
私有void addBottomDots(int currentPage){
if(dotsLayout!=null){
dotsLayout.removeallview();
}
点=新的图像视图[布局.长度];
对于(int i=0;i=Build.VERSION\u code.KITKAT){
windoww=getWindow();
w、 setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
私有void setupViewPager(){
ViewPagerAdapter ViewPagerAdapter=新的ViewPagerAdapter(getSupportFragmentManager(),3);
演练1演练1=新演练1();
演练2演练2=新演练2();
演练3演练3=新演练3();
viewPagerAdapter.addFragment(演练1);
viewPagerAdapter.addFragment(演练2);
viewPagerAdapter.addFragment(演练3);
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(新的viewPager.OnPageChangeListener(){
@凌驾
已滚动页面上的公共无效(int-position、float-positionOffset、int-positionOffsetPixels){
}
@凌驾
已选择页面上的公共无效(内部位置){
添加点(位置);
mCurrentPage=位置;
//更改“下一步”按钮文本“下一步”/“明白了”
如果(位置==layouts.length-1){
btn_next.setText(getString(R.string.start));
}否则{
btn_next.setText(getString(R.string.next));
}
}
@凌驾
公共无效onPageScrollStateChanged(int状态){
}
});
}
@OnClick(R.id.btn\u下一步)
作废下一页(){
Toast.makeText(getApplicationContext(),“顶部填充:+topPadding+”和底部填充“+bottomPadding,Toast.LENGTH_SHORT).show();
mCurrentPage=getItem(+1);
if(mCurrentPage
} ` 我想要的
图像链接

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

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    val topPadding = view.rootWindowInsets.systemWindowInsetTop
    val bottomPadding = view.rootWindowInsets.systemWindowInsetBottom
}
但只有当视图附加到窗口时,才能访问RootWindowInset。在以下回调中获取RootWindowInset是安全的:

view.addOnAttachStateChangeListener(...)


android:fitsystemwindows=“true”
仅适用于某些布局父级,而不是所有布局父级。。。它适用于协调器布局和约束布局。

我忘了补充一点,在视图附加到窗口之前,您不能这样做。您可以使用回调,例如view.addOnAttachStateChangeListener或ViewCompat.SetOnApplyWindowInsetListener,也可以尝试view.post{}setContentView(R.layout.activity_演练);view=relativeLayout.getRootView();view.setonapplyWindowInsertsListener((v,insets)->{if(Build.VERSION.SDK_INT>=Build.VERSION_code.KITKAT){topPadding=view.getRootWindowInsets().GetSystemWindowInsertop();bottomPadding=view.getRootWindowInsets().GetSystemWindowInsertBottom();}返回null;});原因:java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“android.view.view android.view.view.getRootView()”。请移动视图=relativeLayout.getRootView();在需要访问的时候,将其放入回调或使用另一个视图
ViewCompat.setOnApplyWindowInsetsListener(...)