简体   繁体   中英

Constraints not centered with toolbar

Is it posible to avoid inherit view measurements in ConstraintLayout? I mean I have one Activity displaying a toolbar and a Frame container to display Fragments. This fragment is vertically and horizontally centred but when adding toolbar everything moves down.

So.. Is there any way to achieve this?

It depends on the parent Container. You can use

  • ConstraintLayout
  • RelativeLayout
  • MotionLayout

or other relative type layouts, but you can't use LinearLayout as that won't allow you to move children into other sections of the LinearLayout.

Next for the Parent Activity you need to make sure that you are not putting the FrameLayout beneath the Toolbar if you intend to allow yourself to build constraints off the parent boundaries. Your parent is the FrameLayout, so if it is already pushed down beneath the Toolbar you won't be able to bind to it's parent unless you get parent.parent for your binding.

So you have a few options.

  1. Ensure your Activity does not push the FrameLayout beneath the Toolbar so that the FrameLayout is match parent all the way around not affected by the Toolbar (be careful as you will have to make sure other layouts don't overlap the toolbar area in your UI as it could look bad.
  2. You could use a negative margin on UI that needs to float up, this is not good practice and I don't recommend it.
  3. You could build a constraint to the parent.parent because then your constraint is tied to the framelayouts parent instead of the framelayout itself.

If you need more help, then share your XML so I can help point out where you can make adjustments.

Happy Coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM