【问题标题】:Set margin dynamically动态设置边距
【发布时间】:2021-08-06 10:35:42
【问题描述】:

我试图使状态栏半透明,但不希望我的活动内容隐藏在它后面。 -> 我必须动态设置上边距...

我试过这样: MainActivty.java:

    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        View view = findViewById(android.R.id.content).getRootView();
        WindowManager.LayoutParams newLayoutParams = (WindowManager.LayoutParams) view.getLayoutParams();
        view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
            @Override
            public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                int x = insets.getSystemWindowInsetTop();
                Log.d(TAG, "onApplyWindowInsets: " + x);
                //newLayoutParams.topMargin = x;

                //newLayoutParams.setMargins(0,x,0,0);
                view.setLayoutParams(newLayoutParams);
                return insets;
            }
        });
        setContentView(view);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

    } 

很遗憾,这不起作用。 错误信息:

E/AndroidRuntime: 致命异常: main 进程:com.rxstudios.rxmusic2,PID:6324 java.lang.ClassCastException: android.view.WindowManager$LayoutParams 不能转换为 androidx.constraintlayout.widget.ConstraintLayout$LayoutParams 在 com.rxstudios.rxmusic2.MainActivity.onAttachedToWindow(MainActivity.java:77) 在 androidx.appcompat.view.WindowCallbackWrapper.onAttachedToWindow(WindowCallbackWrapper.java:129) 在 com.android.internal.policy.DecorView.onAttachedToWindow(DecorView.java:1537) 在 android.view.View.dispatchAttachedToWindow(View.java:18347) 在 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3397) 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1761) 在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460) 在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183) 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:949) 在 android.view.Choreographer.doCallbacks(Choreographer.java:761) 在 android.view.Choreographer.doFrame(Choreographer.java:696) 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935) 在 android.os.Handler.handleCallback(Handler.java:873) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6669) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

如果它不能让我得到我的 Constraintlayout,我还能怎么做? 既然是constraintlayout,就想着拿到它的第一个child,也就是一个fragment。我可以为片段设置边距吗?如果是,怎么做?

【问题讨论】:

    标签: java android layout native


    【解决方案1】:

    java.lang.ClassCastException: android.view.WindowManager$LayoutParams 无法转换为 androidx.constraintlayout.widget.ConstraintLayout$LayoutParams

    更改代码:

        public void onAttachedToWindow() {
            super.onAttachedToWindow();
            View view = findViewById(R.id.content).getRootView();
            // TODO: 2021/5/17  
    //        WindowManager.LayoutParams newLayoutParams = (WindowManager.LayoutParams) view.getLayoutParams();
            ConstraintLayout.LayoutParams newLayoutParams = (ConstraintLayout.LayoutParams) view.getLayoutParams();
            view.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
     
                @Override
                public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                    int x = insets.getSystemWindowInsetTop();
                    Log.d(TAG, "onApplyWindowInsets: " + x);
                    //newLayoutParams.topMargin = x;
    
                    //newLayoutParams.setMargins(0,x,0,0);
                    view.setLayoutParams(newLayoutParams);
                    return insets;
                }
            });
            setContentView(view);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    

    或者不改java代码,把R.id.content的视图改成LinearLayout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-19
      • 2011-10-28
      • 2012-10-01
      • 2015-07-06
      • 2016-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多