Tôi đang cố gắng thêm ImageView lập trình bên trong LinearLayout, có định hướng thẳng đứng. file layout của tôi là mô tả như xml dưới đây:Làm thế nào để lập trình thêm ImageView trong LinearLayout mà không cần thêm không gian?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:id="@+id/mainView"">
<TextView android:id="@+id/tvTituloInformacao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/white"
android:gravity="center_horizontal"
android:shadowColor="@color/black_translucent"
android:shadowDx="2.0"
android:shadowDy="2.0"
android:shadowRadius="3.0"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<LinearLayout android:id="@+id/resourceContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginBottom="10dip"
android:padding="3dip"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_below="@+id/tvTituloInformacao"
android:background="@color/black_translucent2" />
</RelativeLayout>
Và mã nguồn java của tôi có thêm các ImageViews đến LinearLayout là như sau:
for(Resource r : mUser.getPictures()) {
ImageView img = new ImageView(this);
img.setVisibility(View.VISIBLE);
img.setTag(r.getThumb60());
imageLoader.DisplayImage(r.getThumb60(), this, img);
((LinearLayout) tempFotosView.findViewById(R.id.resourceContainer)).addView(img);
}
Điều gì xảy ra trước và sau mỗi lần ImageView tôi đang nhận được một số không gian phụ không nên ở đó, vì vậy chiều cao của container hình ảnh tăng lên rất nhiều. Một thử nghiệm mà tôi đã làm và nhận được kết quả mong đợi là không sử dụng imageLoader và thay vào đó tôi đặt ImageResource thành một tệp hình ảnh tĩnh. Bằng cách này tôi đã không nhận được thêm bất kỳ không gian nào.
Kết quả kỳ lạ được minh họa trong hình dưới đây:
Có cách nào để không thêm không gian thêm này?
Bạn nên lưu bộ nhớ cache LinearLayout bên ngoài vòng lặp For để tăng hiệu suất. – Subby