FrameLayout margin not working

后端 未结 7 1873
野的像风
野的像风 2020-12-04 09:46

My layout structure is like this

LinearLayout
    FrameLayout
       ImageView
       ImageView
    FrameLayout
    TextView
LinearLayout

I

相关标签:
7条回答
  • 2020-12-04 09:58

    you have to set your ImageView's layout gravity top i.e. android:layout_gravity="top" in the XML resource file, or from code: FrameLayout.LayoutParams.gravity = Gravity.TOP

    0 讨论(0)
  • 2020-12-04 10:00

    add your xml this attribute and re run

    android:layout_gravity="top"

    everything is Ok!

    and you dont set new layout params like this;

    FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(WallpapersActivity.ScreenWidth/2, layH);
    

    use like this:

    FrameLayout.LayoutParams llp = (LayoutParams) myFrameLay.getLayoutParams();
    llp.height = 100;
    llp.width = 100;
    myFrameLay.setLayoutParams(llp);
    
    0 讨论(0)
  • 2020-12-04 10:01

    Have you tried android:layout_gravity ? Try using android:padding in you ImageViews instead of android:layout_margin. AFAIK margins doesn't work properly on Frame layout. I even had to write custom layout for that purpose once. BTW, how do you want allign you ImageViews?

    0 讨论(0)
  • 2020-12-04 10:13

    Taken from the FrameLayout docs (link)

    The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits).

    This seems to describe the fact that it'll strip margins out. Like boulder mentioned, you could try switching to padding as it can be used to produce a similar effect if done properly.

    Out of curiosity, you mentioned that it does work fine when using a LinearLayout container, why the choice of FrameLayout?

    0 讨论(0)
  • 2020-12-04 10:15

    I had the same issue myself and noticed that setting the layout_ margins does not work until you also set your ImageView's layout gravity i.e. android:layout_gravity="top" in the XML resource file, or from code: FrameLayout.LayoutParams.gravity = Gravity.TOP;.

    0 讨论(0)
  • 2020-12-04 10:24

    try setCropToPadding(true) to ImageView ,this should be helped!

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