Android center view in FrameLayout doesn't work

前端 未结 5 1733
梦谈多话
梦谈多话 2021-02-04 22:47

I have a FrameLayout in which I have 2 controls: - a custom view which draws a image and some text on it - a textview with a text

I want to center both in the FrameLayo

5条回答
  •  暖寄归人
    2021-02-04 23:27

    We can align a view in center of the FrameLayout by setting the layout_gravity of the child view.

    In XML:

    android:layout_gravity="center"
    

    In Java code:

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER;
    

    Note: use FrameLayout.LayoutParams not the others existing LayoutParams

提交回复
热议问题