android imageview image center technique application

  • 2020-05-07 20:23:39
  • OfStack

When making UI layout, especially when encountering complex multiple LinearLayout nesting, I was often troubled by some small problems for half a day. For example, when using ImageView today, I wanted to make it center display, but no matter how to set layout_gravity property, I could not achieve the effect. Part of the code is as follows:
[java]
 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:layout_weight="1" 
android:padding="20dp" > 
<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 
<LinearLayout 
android:layout_width="108dp" 
android:layout_height="108dp" 
android:orientation="vertical" 
android:background="#3399ff"> 
<ImageView 
android:layout_width="64dp" 
android:layout_height="64dp" 
android:src="@drawable/menu_icon__mail" 
android:layout_gravity="center"/> 
</LinearLayout> 

Therefore, the way to find the data in 4 places for solution is the original reason that the attributes of the superclass view are not set. Setting the superclass view as the center edge can be solved, that is, android:gravity="center" :
[java]
 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:layout_weight="1" 
android:padding="20dp" > 
<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 
<LinearLayout 
android:layout_width="108dp" 
android:layout_height="108dp" 
android:orientation="vertical" 
android:gravity="center" 
android:background="#3399ff"> 
<ImageView 
android:layout_width="64dp" 
android:layout_height="64dp" 
android:src="@drawable/menu_icon__mail" 
android:layout_gravity="center"/> 
</LinearLayout> 

UI design is often very small details, very simple problems, sometimes will let you trouble for a long time, but many design experience with their own hands, to solve the simple bird!

Related articles: