Place view inside FrameLayout in Android

前端 未结 5 1305
再見小時候
再見小時候 2020-12-31 17:41

I want to add a view inside a FrameLayout programmatically and to place it in a specific point within the layout with a specific width and height. Does FrameLayout support t

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 18:15

    You can also add a margin around the newly added view to position it inside the FrameLayout.

    FrameLayout frameLayout = (FrameLayout) findViewById(R.id.main); // or some other R.id.xxx
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(0, metrics.heightPixels - 20, 0, 0);
    View v = new View(context);
    v.setLayoutParams(params);
    frameLayout.addView(v);
    

    This will position the FrameLayout 20 pixels from the bottom of the screen.

    Edit: completed the example so it stands by itself. And oh, yes it does work.

提交回复
热议问题