您好,登錄后才能下訂單哦!
在Android中,ImageView用于顯示圖片,ViewStub用于延遲加載布局。結(jié)合使用兩者可以實現(xiàn)在需要時動態(tài)加載圖片。
首先在布局文件中,可以將ViewStub和ImageView放在一起,例如:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ViewStub
android:id="@+id/view_stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="@+id/image_layout"
android:layout="@layout/image_layout"/>
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
</LinearLayout>
然后在代碼中,可以根據(jù)需要加載圖片到ImageView中:
ViewStub viewStub = findViewById(R.id.view_stub);
ImageView imageView = findViewById(R.id.image_view);
// 判斷是否需要加載圖片,如果需要則加載
if (needLoadImage) {
// 加載圖片到ImageView中
imageView.setImageResource(R.drawable.image);
imageView.setVisibility(View.VISIBLE);
} else {
// 顯示ViewStub布局,用于延遲加載圖片
viewStub.setLayoutResource(R.layout.image_layout);
View inflatedView = viewStub.inflate();
}
這樣就可以根據(jù)需要動態(tài)加載圖片到ImageView中,同時利用ViewStub實現(xiàn)延遲加載布局,避免不必要的資源消耗。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。