Android:layout_weight is a property of a linear layout (LinearLayout) that represents the weight of the control. This property, which we can use to denote a linear layout in which several controls are displayed proportionally, which of course also includes the average display of the individual controls. This property is, in fact, a very useful interface for UI adaptation.
Google's official recommendation, when using the weight attribute, set the width to 0dip, the effect is the same as set to Wrap_content. This android:layout_weight can be understood as the length ratio of the control.
In this example, we use Android:layout_weight to display an average of three images and two images as a 1:2 ratio.
1.:
2. layout file: 2.1 Layout file for the first row of pictures (three images distributed evenly):
<linearlayout android:id= "@+id/contact_detail_btn_area" android:orientation= "Horizontal" android:l Ayout_width= "Match_parent" android:layout_height= "65DP" > <linearlayout android:id= "@+id/co Ntact_share_btn "android:orientation=" vertical "android:layout_width=" 0DP "android:layout _height= "Wrap_content" android:layout_weight= "1" android:background= "#f7f7f7" Android:gra vity= "center" > <imageview android:layout_width= "wrap_content" Android:layo ut_height= "Wrap_content" android:gravity= "center_vertical" android:layout_margintop= "10DP" android:background= "@drawable/share"/> <textview android:layout_width= "wrap _content "android:layout_height=" wrap_content "android:textsize=" 10SP "Androi d:text= "Share"/> </LinearLayout> <linearlayout android:layout_width= "Wrap_content" android:layou t_height= "wrap_content" android:orientation= "horizontal" android:background= "#f7f7f7" > <view android:layout_width= "1dip" android:layout_height= "Wrap_content" an droid:layout_margintop= "10DP" android:background= "#d1d1d1"/> </LinearLayout> <li Nearlayout android:id= "@+id/contact_edit_btn" android:orientation= "vertical" Android:layo Ut_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" Android:bac Kground= "#f7f7f7" android:gravity= "center" > <imageview android:layout_width= "WR Ap_content "android:layout_height=" wrap_content "android:gravity=" center_vertical " Android:layout_margintop= "10DP" android:background= "@drawable/edit"/> <textview android:layo Ut_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "10SP" android:text= "edit"/> </LinearLayout> <linearlayout android:layout_width= "W Rap_content "android:layout_height=" wrap_content "android:orientation=" Horizontal "Androi d:background= "#f7f7f7" > <view android:layout_width= "1dip" Android:layout_h eight= "Wrap_content" android:layout_margintop= "10DP" android:background= "#d1d1d1"/> </LinearLayout> <linearlayout android:id= "@+id/contact_menu_btn" Android:orienta tion= "vertical" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:l ayout_weight= "1" Android:background= "#f7f7f7" android:gravity= "center" > <imageview android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:gravity= "Center_ver Tical "android:layout_margintop=" 10DP "android:background=" @drawable/menu "/> & Lt TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textsize= "10SP" android:text= "menu"/> </LinearLayout> </linearlayout> ;
2.2 Layout file for the second row of pictures (two images by 1:2 distribution):
<linearlayout android:orientation= "Horizontal" android:layout_width= "Match_parent" Android:layout_ height= "65DP" > <linearlayout android:orientation= "vertical" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:background= "#f7f7f 7 "android:gravity=" Center "> <imageview android:layout_width=" wrap_content " android:layout_height= "Wrap_content" android:gravity= "center_vertical" android:l ayout_margintop= "10DP" android:background= "@drawable/share"/> <textview an Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "10s P "android:text=" share "/> </LinearLayout> <linearlayout android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:orientation= "Horizontal" android:background= "# F7f7f7 "> <view android:layout_width=" 1dip "android:layout_height=" Wrap_con Tent "android:layout_margintop=" 10DP "android:background=" #d1d1d1 "/> </linearl ayout> <linearlayout android:orientation= "vertical" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "2" android:background= "#f7f7f7" android:gravity= "center" > <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:gravity= "center_vertical" Android:layout_mar gintop= "10DP" android:background= "@drawable/edit"/> <textview android:layo Ut_width= "Wrap_content" android:layout_height= "Wrap_content" android:textsize= "10sp" android:text= "Ed It "/> </LinearLayout></LinearLayout>
2.3: Entire layout file:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:orien tation= "vertical" ><linearlayout android:id= "@+id/contact_detail_btn_area" android:orientation= "Horizo Ntal "android:layout_width=" match_parent "android:layout_height=" 65DP "> <linearlayout Android:id= "@+id/contact_share_btn" android:orientation= "vertical" android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:background= "#f7f7f7" android:gravity= "center" > <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:gravity= "center_vertical" Android:lay out_margintop= "10DP" Android:background= "@drawable/share"/> <textview android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:textsize= "10sp" android:text= "Share"/> </LinearLayout> <linearlayout android:layout_width= "Wrap_content" android:layout _height= "wrap_content" android:orientation= "horizontal" android:background= "#f7f7f7" > <view android:layout_width= "1dip" android:layout_height= "Wrap_content" and roid:layout_margintop= "10DP" android:background= "#d1d1d1"/> </LinearLayout> <lin Earlayout android:id= "@+id/contact_edit_btn" android:orientation= "vertical" android:layou T_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" android:back Ground= "#f7f7f7" android:gravity= "center" > <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:gravity= "center_vertical" Android:layout_marg intop= "10DP" android:background= "@drawable/edit"/> <textview android:layou T_width= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "10SP" android:text= "edit"/> </LinearLayout> <linearlayout android:layout_width= "WR Ap_content "android:layout_height=" wrap_content "android:orientation=" Horizontal "Android : background= "#f7f7f7" > <view android:layout_width= "1dip" android:layout_he ight= "Wrap_content" android:layout_margintop= "10DP" android:background= "#d1d1d1"/> </LinearLayout> <linearlayout android:id= "@+id/contact_menu_btn" android:orientation= "vertical" Android:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" Android:background= "#f7f7f7" android:gravity= "center" > <imageview android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:gravity= "Center_vert ical "android:layout_margintop=" 10DP "android:background=" @drawable/menu "/> &L T TextView android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textsize= "10SP" android:text= "menu"/> </LinearLayout> </linearlayout> ; <linearlayout android:orientation= "Horizontal" android:layout_width= "Match_parent" Android:layout_ height= "65DP" > <linearlayout android:orientation= "vertical" android:layout_width= "0DP" Android : layout_height= "Wrap_content" android:layout_weight= "1" android:background= "#f7f7f7" Android oid:gravity= "center" > <imageview android:layout_width= "wrap_content" Androi d:layout_height= "Wrap_content" android:gravity= "center_vertical" android:layout_margintop= "1 0DP "android:background=" @drawable/share "/> <textview android:layout_width = "Wrap_content" android:layout_height= "wrap_content" android:textsize= "10SP" android:text= "Share"/> </LinearLayout> <linearlayout android:layout_width= "Wrap_conte NT "android:layout_height=" wrap_content "android:orientation=" Horizontal "Android:backgro und= "#f7f7f7" > <view android:layout_width= "1dip" android:layout_height= "Wrap_content" android:layout_margintop= "10DP" android:background= "#d1d1d1"/> </LinearLayout> &l T LinearLayout android:orientation= "vertical" android:layout_width= "0DP" Android:layout_hei ght= "Wrap_content" android:layout_weight= "2" android:background= "#f7f7f7" android:gravity = "center" > <imageview android:layout_width= "wrap_content" android:layout_he ight= "Wrap_content" android:gravity= "center_vertical" android:layout_margintop= "10DP" android:background= "@drawable/edit"/> <textview android:layout_width= "Wrap_conte NT "android:layout_height=" Wrap_content "android:textsize=" 10SP "Android:text = "Edit"/> ≪/linearlayout></linearlayout> </LinearLayout>
The use of Android Android:layout_weight