android layout proportional layout of code

  • 2020-05-09 19:20:02
  • OfStack

To create a scaled child View, set the width and height of LinearLayout to fill_parent, the width or height of child View to 0, and then set different weights for child View (weight), so that the size of View will be proportional.

This example USES horizontal LinearLayout, android of LinearLayout :layout_width= "match_parent" to indicate that the entire screen width will be used.

For several children of LinearLayout, View, define their width as 0, android:layout_width= "0dip", and then specify the width ratio for each View using layout_weight. In this case, each TextView USES the same weight, so all four TextView will have the same width. This makes the height of TextView multi-line for those displaying longer text.


<LinearLayout xmlns:android= " http://schemas.android.com/apk/res/android " android:orientation= " horizontal " 
android:layout_width= " match_parent " 
android:layout_height= " wrap_content " >
<TextView
android:background= " @drawable/red " 
android:layout_width= " 0dip " 
android:layout_height= " match_parent " 
android:layout_weight= " 1 " 
android:text= " @string/linear_layout_7_small " />
<TextView
android:background= " @drawable/green " 
android:layout_width= " 0dip " 
android:layout_height= " match_parent " 
android:layout_weight= " 1 " 
android:text= " @string/linear_layout_7_big " />
<TextView
android:background= " @drawable/blue " 
android:layout_width= " 0dip " 
android:layout_height= " match_parent " 
android:layout_weight= " 1 " 
android:text= " @string/linear_layout_7_small "  />
<TextView
android:background= " @drawable/yellow " 
android:layout_width= " 0dip " 
android:layout_height= " wrap_content " 
android:layout_weight= " 1 " 
android:text= " @string/linear_layout_7_wrap " />
</LinearLayout>


Related articles: