Create Layout Programmatically in android - issue

前端 未结 2 812
死守一世寂寞
死守一世寂寞 2021-02-20 14:10

I am creating FrameLayout dynamically using the following code

mylayout.java

FrameLayout layout = new FrameLayout(this);
FrameLayout.LayoutParams layoutp         


        
相关标签:
2条回答
  • 2021-02-20 14:33

    Add

    layout.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
    

    or

    layout.setGravity(Gravity.CENTER);
    

    You forgot to set the gravity of the layout.

    0 讨论(0)
  • 2021-02-20 14:38

    Android Developer - Frame Layout Documentation reads:

    Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

    In your case, you may use layout.setForegroundGravity(int) for the same.

    0 讨论(0)
提交回复
热议问题