There is a layout, it has a view.


This layout is drawn on the whole screen, how to make it so that it is drawn only up to the size of the inscribed view.

And how to make the view inscribed in it centered on the center of the screen (directly or indirectly, it doesn't matter)

Generally speaking, for some reason, my layout occupies the entire screen, and view occupies the entire layout and thus also fills the entire screen, but I want the layout to be as large as the view inside it, and drawn in a specific place, the layout is generated by java code (not from xml).

  • Nicho is not clear - more detail please - Barmaley

2 answers 2

It depends on where your layout is set, if you are xml, then like this, and in it you already place the view you need:

 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > </LinearLayout> 

In the code, you need to understand in which container we have it, or use ViewGroup.LayoutParams, I would rather use the first option. Assume that the layout will be in RelativeLayout (for the rest of the layouts it will be similar)

 layout = new LinearLayout(context); layout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); //и добавляете свою view в layout layout.addView(yourView); 

    Do you need to dynamically build a UI based on some data? If so, it is better to make nested (read, child) view-hee in xml layouts. Then, when building a UI (for example, a form or some other content), simply inflate the child views, transfer the necessary data to them and attach to the root layout.