Two methods of dynamically adding views in layout based on Android (summary)
1、 Explain
There are two ways to add view files: 1. Define layout in XML file; 2. Java code writing
2、 Preface description
1. Construct XML file
2.LayoutInflater
When it comes to addview, you first need to understand the layoutinflator class. The main function of this class is to convert the layout expressed in XML into view. For ease of understanding, we can compare it with findviewbyid(). Both of them instantiate an object. The difference is that findviewbyid() instantiates a specific widget control under the XML layout file, while layoutinflator instantiates it through the XML layout file under RES / layout /.
(1) Create
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); or
LayoutInflater inflater = LayoutInflater.from(Activity.this); or
LayoutInflater inflater = getLayoutInflater();
The three methods are essentially the same.
(2)inflate()
Convert the layout file to view with layoutinflator. Inflate().
View view = inflater.inflate(R.layout.block_gym_album_list_item,null);
3. Add view file
3、 Steps
1. By defining layout in XML file (block_gym_album_list_item. XML)
activity_ dynamic
3、MainActivity
The above two methods (summary) of dynamically adding views to the layout based on Android are all the contents shared by Xiaobian. I hope to give you a reference and support more programming tips.