我有布局来显示来自用户的galery的加载图像列表。首先,在布局中有默认的ImageView。如果用户加载新图像,我得到默认图像的LayoutParam然后删除默认的ImageView,然后将复制的LayoutParam放到新加载的图像,然后将ImageView放到屏幕上。问题是新的ImageView没有显示任何内容,而我重复了默认的ImageView的LayoutParam。这是代码。
activity_view_product.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginBottom="16dp"
android:baselineAligned="false"
android:orientation="horizontal">
<HorizontalScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#eee">
<LinearLayout
android:id="@+id/lyImage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/ivNoImage"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="@drawable/noimage"/>
</LinearLayout>
</HorizontalScrollView>
<ImageButton
android:id="@+id/ibAddImage"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/ic_add_white_24dp"/>
</LinearLayout>
ViewProductActivity.java
private void chooseImageFromGalery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri imageUri = data.getData();
ImageView ivImage = new ImageView(this);
int size = ((int) getResources().getDisplayMetrics().density)*75;
LinearLayout.LayoutParams lyParam = (LinearLayout.LayoutParams) findViewById(R.id.ivNoImage).getLayoutParams();
//LinearLayout.LayoutParams lyParam = new LinearLayout.LayoutParams(75, 75, 1);
Log.i("zihad", "layout param : "+lyParam.width);
Log.i("zihad", "layout param : "+lyParam.height);
Log.i("zihad", "layout param : "+lyParam.weight);
Glide.with(ViewProductActivity.this)
.load(imageUri)
.error(R.drawable.noimage)
.signature(new StringSignature(String.valueOf(System.currentTimeMillis())))
.into(ivImage);
lyImage.addView(ivImage, lyParam);
ivImage.setAdjustViewBounds(true);
try {
if (imageLoadedBitmap.size() == 0) {
lyImage.removeView(findViewById(R.id.ivNoImage));
imageLoadedBitmap.add(MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}