android中对layout设置背景可以有“静态“和“动态“两种方式。
所谓“静态“,指的是在layout的xml文件中指定,如:
<com.intel.camera22.ui.RotateImageView
android:id="@+id/shortcut_mode_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawble/image1 />
这种方式,当layout inflate的同时,设置layout的背景。
所谓“动态“,指在代码中指定layout的背景,如:
<com.intel.camera22.ShutterButton
android:id="@+id/shutter_button"
android:layout_width="198dp"
android:layout_height="198dp"
android:clickable="true"
android:contentDescription="@string/accessibility_shutter_button"
android:focusable="true"
android:padding="10dp"
android:layout_gravity="center" />
此处没有指定image src。在java代码中,可以作如下设置:
mShutterButton = (ShutterButton) findViewById(R.id.shutter_button);
mShutterButton.setImageResource(R.drawable.btn_shutter);
注意,有些场合是不适合用“静态“设置的方式的,否则在场景切换的过程中,将有可能看到一些预期设计中所没有的“额外的“UI:这些都是因为在xml文件中指定了背景图片的原因。