简体   繁体   中英

Align imageview to bottom right in FrameLayout

I am using below code to align imageview to bottom right with margin of 20,10 inside FrameLayout but it is appearing at top left.Where am I going wrong?

 ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(refreshBtn.getLayoutParams());
    marginParams.setMargins(0, 0, 20, 10);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(marginParams);
layoutParams.gravity=Gravity.RIGHT|Gravity.BOTTOM;
refreshBtn.setLayoutParams(layoutParams);

The problem is that you are changing the layout gravity, meaning that you are positioning the FrameLayout in the bottom right corner. However, it's possible that the FrameLayout is filling the whole screen in which case the layout gravity doesn't really change anything. Imagine a book perfectly fitting in a drawer. No matter how you're holding the drawer, the book doesn't move inside it.

What you have to do is use the "setGravity()" method of the FrameLayout itself. This will change the actual gravity inside the FrameLayout.

Source: How to set both gravity and layout gravity of a LinearLayout programatically

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